我开始使用GCD,我需要知道某个线程何时结束了它的工作。
我的代码:
dispatch_queue_t registerDeviceQueue = dispatch_queue_create("RegisterDevice", NULL);
dispatch_async(registerDeviceQueue, ^{
[self registerDevice];
dispatch_async(dispatch_get_main_queue(), ^{
[aiRegisterDevice stopAnimating];
});
});
dispatch_release(registerDeviceQueue);
我需要知道这个步骤何时结束,以便UIActivityView可以停止。 它现在的方式,它在线程结束前停止。
谢谢,
RL
答案 0 :(得分:6)
我设置了一个组并使用dispatch_group_wait
仅在计算完成时继续。举个例子:
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);
或者,如果您想阻止回调阻止使用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);