问题描述
我需要将多个任务发送到Grand Central Dispatch才能运行.有些任务将首先完成,而有些任务将最后完成.
I need to send multiple tasks to Grand Central Dispatch to run. Some tasks will finish first, while some will finish last.
我怎么知道我在Grand Central Dispatch中完成的所有任务?
How do I know all my tasks in Grand Central Dispatch finished?
我应该使用计数器记录完成的任务数吗?还有更聪明的方法吗?
Should I use a counter to record the number of tasks finished? Any smarter method?
推荐答案
您可以使用调度组在所有任务完成时得到通知.这是来自 http://cocoasamurai的示例. blogspot.com/2009/09/guide-to-blocks-grand-central-dispatch.html
You can use dispatch groups to be notified when all tasks completed. This is an example from http://cocoasamurai.blogspot.com/2009/09/guide-to-blocks-grand-central-dispatch.html
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,queue,^{
NSLog(@"Block 1");
});
dispatch_group_async(group,queue,^{
NSLog(@"Block 2");
});
dispatch_group_notify(group,queue,^{
NSLog(@"Final block is executed last after 1 and 2");
});
这篇关于我怎么知道我在Grand Central Dispatch中完成的所有任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!