在后台线程上保存托管对象上下文时,我正在侦听NSManagedObjectContextDidSaveNotification并尝试在主线程上合并它。
但是,当我尝试将请求转发到主线程时,既没有使用
[self performSelectorOnMainThread:@selector(executeThisCode:) withObject:saveNotification waitUntilDone:NO]; 也不是
dispatch_async(dispatch_get_main_queue(), ^{
...execute this code
});奇怪的是,这一切都适用于iOS 5.1和iOS 5.0,但不适用于iOS 6。你有什么想法吗?
发布于 2012-10-23 23:47:40
你会先检查你是否已经在主线程中了吗?如果在调用performSelectorOnMainThread时,executeThisCode是当前正在执行的方法的选择器,那么这将是特别相关的。如下所示:
- (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
}https://stackoverflow.com/questions/13033785
复制相似问题