您可以将 loadAlert 调用移动到 alertThreadMethod 或使用Grand Central Dispatch串行队列,例如,
dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_async(queue, ^{
[self alertThreadMethod];
[self loadAlert];
});
dispatch_release(queue);
或者,如果 loadAlert 正在更新 UI,因为您在主队列中进行 UI 更新,您将执行以下操作:
dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_async(queue, ^{
[self alertThreadMethod];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadAlert];
});
});
dispatch_release(queue);
顺便说一句,如果您只是在后台执行这项任务,而不是创建自己的串行队列,您可能只使用现有的后台队列之一。如果您需要串行性质,您只需要创建一个队列(即您将有大量的 dispatch_async 调用并且您不能让它们同时运行)。但在这个简单的例子中,这可能会更高效一些,绕过串行队列的创建和释放:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
[self alertThreadMethod];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadAlert];
});
});