[英]Cannot invoke code on the main thread from the background thread in iOS6
当在后台线程上保存托管对象上下文时,我在听NSManagedObjectContextDidSaveNotification并尝试在主线程上合并它。
但是,当我尝试将请求转发到主线程时,都不会执行任何代码
[self performSelectorOnMainThread:@selector(executeThisCode:) withObject:saveNotification waitUntilDone:NO];
也没有
dispatch_async(dispatch_get_main_queue(), ^{
...execute this code
});
奇怪的是,这一切都适用于iOS 5.1和iOS 5.0,但不适用于iOS6。有什么想法吗?
首先检查是否已经在主线程中吗? 如果executeThisCode
是您在调用performSelectorOnMainThread
时当前正在执行的方法的选择器,则这将特别相关。 像这样:
- (void) executeThisCode: (NSNotification*) notification{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(executeThisCode:)
withObject:notification
waitUntilDone:YES];
return;
}
// merge logic goes here and executes on the main thread
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.