我尝试使用以下代码按顺序执行backgroup中的2个任务:
var group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
println("start task 1");
NSThread.sleepForTimeInterval(10000);
});
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
println("start task 2");
NSThread.sleepForTimeInterval(10000);
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
当运行任务1和任务2同时执行时,而不是inqequence。
我做错了什么?
答案 0 :(得分:0)
因为全局队列 CONCURRENT 队列,如果您希望任务按顺序执行,请创建串行队列
let serialQueue = dispatch_queue_create("com.test.helloHwc",DISPATCH_QUEUE_SERIAL)
dispatch_group_async(group,serialQueue, {
println("start task 1");
NSThread.sleepForTimeInterval(10);
});
dispatch_group_async(group,serialQueue, {
println("start task 2");
NSThread.sleepForTimeInterval(10);
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
使用文档更新
提交到串行队列的块按FIFO顺序一次执行一个。但是,请注意,提交给独立队列的块可以相互同时执行。提交到并发队列的块以FIFO顺序出列,但如果有资源可以同时运行