Ios 异步任务队列不存在';t工作(SWIFT)
我尝试在后台组中按以下代码顺序执行2个任务:Ios 异步任务队列不存在';t工作(SWIFT),ios,swift,asynchronous,Ios,Swift,Asynchronous,我尝试在后台组中按以下代码顺序执行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); })
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时,任务1和任务2同时执行,而不是按顺序执行
我做错了什么?因为全局队列是并发队列,如果希望任务按顺序执行,请创建串行队列
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顺序退出队列,但如果资源可用,则可以并发运行