当我从 secondView 返回到 mainView 时,我正在 secondView 的 viewDidDisappear 方法中处理某些内容。问题是,由于应用程序必须执行的工作,我的 mainView 卡住了。
这是我的做法:
-(void)viewDidDisappear:(BOOL)animated
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dbq = [[dbqueries alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"abc" object:nil];
//the notification should start a progressView, but because the whole view gets stuck, I can't see it working, because it stops as soon as the work is done
dispatch_sync(dispatch_get_main_queue(), ^{
//work
});
});
我做错了什么? 提前致谢!
最佳答案
您需要在 dispatch_async
中执行您的工作到您的 queue
。您目前正在主线程中执行工作(假设 //Work
注释就在它所在的位置),此外,您正在阻塞工作线程等待这项工作。
尝试稍微重新安排您的 GCD 调用:
-(void)viewDidDisappear:(BOOL)animated
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dbq = [[dbqueries alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"abc" object:nil];
dispatch_async(queue, ^{
// Perform work here
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI here
});
});
}
关于ios - 即使我使用 GCD,UI 也会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11541638/