// 代码框架结构如下:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 耗时的操作
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
});
});
// 1.Dispatch After
// 主要用于延迟执行一些代码。
// 例子:
int64_t delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self dismissModalViewControllerAnimated:YES];
});
// 说明:首先声明一个时间,之后从现在开始计时,一旦过了特定的时间后就执行after代码块中的内容。
// 2.Dispatch Once
// 只执行一次,用于一些单例。
// 例子:
static SKTraktAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
});
return _sharedClient;
// _sharedClient是一个单例,如果已经存在,不需要重复创建。
// GCD
// 1 Serial queue 串行 虽然串行队列是在一个分线程中执行,但是如果有多个串行队列,则他们相互之间的并行的。
// 调度 队列 创建
// 第一个参数:唯一标识队列的标识符,c的字符串
// 第二个参数:NULL , DISPATCH_QUEUE_SERIAL(串行) or DISPATCH_QUEUE_CONCURRENT(并行)
// 2、dispatch_group_async的使用
// dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。这个方法很有用,比如你执行三个下载任务,当三个任务都下载完成后你才通知界面说完成的了。下面是一段例子代码:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:1]; NSLog(@"hhidoos1"); }); dispatch_group_async(group, queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"group2"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"group3"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"updateUi"); }); dispatch_release(group);
// 3、dispatch_barrier_async的使用 // dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行 //
// 例子代码如下:
dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"dispatch_async1"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:4]; NSLog(@"dispatch_async2"); }); dispatch_barrier_async(queue, ^{ NSLog(@"dispatch_barrier_async"); [NSThread sleepForTimeInterval:4]; }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"dispatch_async3"); });
// 打印结果: // 2012-09-25 16:20:33.967 gcdTest[45547:11203] dispatch_async1 // 2012-09-25 16:20:35.967 gcdTest[45547:11303] dispatch_async2 // 2012-09-25 16:20:35.967 gcdTest[45547:11303] dispatch_barrier_async // 2012-09-25 16:20:40.970 gcdTest[45547:11303] dispatch_async3 // 请注意执行的时间,可以看到执行的顺序如上所述。
// 4、dispatch_apply // 执行某个代码片段N次。
dispatch_queue_t queue=dispatch_get_global_queue(0, 0);
dispatch_apply(10, queue, ^(size_t index) {
NSLog(@"%zu",index);
});
#pragma mark 定时器
-@property (nonatomic) dispatch_source_t timerSource;
(void)puslocation
{
dispatch_queue_t timerQueue = dispatch_queue_create("timerQueue", 0);
/// 创建 gcd timer.
_timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerQueue);
double interval = 60 * NSEC_PER_SEC; /// 间隔2秒
dispatch_source_set_timer(_timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);
/// 定时器block设置
dispatch_source_set_event_handler(_timerSource, ^{
NSLog(@" ======\n\n%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
});
});
/// 唤起定时器任务.
dispatch_resume(_timerSource);
}