#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)異步
- (void)viewDidLoadasync
{ide
[superviewDidLoad];函數
dispatch_async(kBgQueue, ^{fetch
NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];spa
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:datawaitUntilDone:YES];3d
});orm
}隊列
dispatch_async會向kBgQueue隊列中添加新的任務去執行,這裏kBgQueue隊列使用dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)得到。get
Dispatch Queues
Dispatch Queues從使用的角度將更象另外一種形式的Operation Queues只是 Operation Queuse是用ObjectC的Dispatch Queues是C的
dispatch Queues有serial Queues 也被稱爲私有dispatch Queues,一個時間只能運行一個task,順序運行
dispatch_queue_t queue;
queue = dispatch_queue_create("myQueue", NULL);
dispatch_async(queue, ^{
printf("Do some work here.\n");
});
printf("The first block may or may not have run.\n");
dispatch_sync(queue, ^{
printf("Do some more work here.\n");
});
printf("Both blocks have completed.\n");
這裏使用了同步dispatch和異步dispatch,推薦使用dispatch_async這樣才能真正體現其中的優點同步至關於WaitUntil =YES
還有一種就是Concurrent Queues每一個程序系統自動提供了3個Concurrent Queues
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t aHQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t aLQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
啥意思一看就明白,3個優先級別的concurrent queues
最後一個特殊的Dispatch Queue就是main dispatch Queue 也是程序啓動自動生成
dispatch_queue_t mainQueue = dispatch_get_main_queue();
concurrent queues和main queue 都是由系統生成並且 dispatch_suspend, dispatch_resume, dispatch_set_context,這些函數對他們無效
可是咱們的應用不是簡單的同步也異步的運行,應用常常是混合的
好比咱們要task1 task2 task3 都運行完成後才能異步運行task4 task5 task6咱們該怎麼作呢?這裏咱們能夠引入group的概念
dispatch_queue_t aDQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
// Add a task to the group
dispatch_group_async(group, aDQueue, ^{
printf("task 1 \n");
});
dispatch_group_async(group, aDQueue, ^{
printf("task 2 \n");
});
dispatch_group_async(group, aDQueue, ^{
printf("task 3 \n");
});
printf("wait 1 2 3 \n");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
printf("task 1 2 3 finished \n");
dispatch_release(group);
group = dispatch_group_create();
// Add a task to the group
dispatch_group_async(group, aDQueue, ^{
printf("task 4 \n");
});
dispatch_group_async(group, aDQueue, ^{
printf("task 5 \n");
});
dispatch_group_async(group, aDQueue, ^{
printf("task 6 \n");
});
printf("wait 4 5 6 \n");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
printf("task 4 5 6 finished \n");
dispatch_release(group);
有時候咱們也能夠將設定一個數據在queue中也能夠定義一個結束函數
dispatch_set_finalizer_f 是在dispatch_release時候被調用
dispatch_queue_t serialQueue = dispatch_queue_create("com.example.CriticalTaskQueue", NULL);
if (serialQueue)
{
dispatch_set_context(serialQueue, self);
dispatch_set_finalizer_f(serialQueue, &myFinalizerFunction);
}
dispatch_group_t group = dispatch_group_create();
// Add a task to the group
dispatch_group_async(group, serialQueue, ^{
printf("task 1 \n");
});
dispatch_group_async(group, serialQueue, ^{
printf("task 2 \n");
});
dispatch_group_async(group, serialQueue, ^{
printf("task 3 \n");
});
printf("wait 1 2 3 \n");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);
dispatch_release(serialQueue);