我在修改线程内的 View 时遇到问题。我尝试添加一个 subview ,但显示需要大约 6 秒或更长时间。我终于让它工作了,但我不知 Prop 体如何。所以我想知道它为什么起作用以及以下方法之间有什么区别:
dispatch_async(dispatch_get_main_queue(), ^{
//some UI methods ej
[view addSubview: otherView];
}
[viewController performSelectorOnMainThread:@selector(methodThatAddsSubview:) withObject:otherView
waitUntilDone:NO];
NSNotification
方法 - 也花了大约 6 秒时间来显示观察者在我想修改的 viewController 中,并与添加 subview 的方法配对。[[NSNotificationCenter defaultCenter] postNotificationName:
@"notification-identifier" object:object];
作为引用,这些是在 ACAccountStore
类的 CompletionHandler
中调用的。
accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
// my methods were here
}
}
最佳答案
默认情况下,-performSelectorOnMainThread:withObject:waitUntilDone:
仅调度选择器以默认运行循环模式运行。如果运行循环处于另一种模式(例如跟踪模式),它不会运行,直到运行循环切换回默认模式。您可以使用变体 -performSelectorOnMainThread:withObject:waitUntilDone:modes:
解决此问题(通过传递您希望它运行的所有模式)。
另一方面,dispatch_async(dispatch_get_main_queue(), ^{ ... })
将在主运行循环将控制流返回到事件循环时立即运行该 block 。它不关心模式。因此,如果您也不想关心模式,dispatch_async()
可能是更好的选择。
关于objective-c - performSelectorOnMainThread : and dispatch_async() on main queue? 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9335434/
相关文章:
objective-c - 将 UIColor 保存到 NSUserDefaults 并从中加载
objective-c - 最大 CGFloat 值是否有常数?
ios - command/usr/bin/codedesign 失败,退出代码 1-代码符号错误
iphone - 如何将 nil 添加到 nsmutablearray?
ios - ld : file not found: linker command failed w
iphone - HTML 内容适合 UIWebview 而无需缩小
objective-c - 主队列上的 dispatch_sync 与 dispatch_async