I'd set up a group and use dispatch_group_wait
to continue only when the computation has finished. To take your example:
dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, registerDeviceQueue, ^{
[self registerDevice];
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); // Block until we're ready
// Now we're good to call it:
[aiRegisterDevice stopAnimating];
dispatch_release(registerDeviceQueue);
dispatch_release(group);
Or, if you want to prevent the callback blocking use dispatch_group_notify
:
dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, registerDeviceQueue, ^{
[self registerDevice];
}); // In this version, the group won't block
// This block gets called asynchronously when the above code is done:
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[aiRegisterDevice stopAnimating];
});
dispatch_release(registerDeviceQueue);
dispatch_release(group);