Using GCD dispatch groups to process data in multiple threads and getting notified when all processing is done.
- (void)doSomethingWithCompletion:(void (^)())completion
{
dispatch_group_t myGroup = dispatch_group_create();
for (id d in [self data])
{
dispatch_group_enter(myGroup);
// Each iteration is done independently, in its own thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Here is where the data is being processed.
[self process:d];
// Here is where the UI is being updated.
dispatch_async(dispatch_get_main_queue(), ^{
// Do what you like with the UI...
dispatch_group_leave(myGroup);
});
});
}
dispatch_group_notify(myGroup, dispatch_get_main_queue(),^{
// All iterations have been completed.
completion();
});
}