我使用队列和结果控制器的组合来更新和显示一些核心数据对象。
在我的 uitableviewcontroller 中,我每隔 X 秒调用一次主控制器对象中的方法。
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test:) userInfo:nil repeats:YES];
}
- (void)test:(NSTimer*)theTimer {
[[MainController instance] updatePersons];
}
在此方法中,自定义 NSOperation 对象将添加到我的主 Q 中。
- (BOOL)updatePersons {
UpdatePersonsOperation* u = [[UpdatePersonsOperation alloc] init];
[u setQueuePriority:NSOperationQueuePriorityVeryHigh];
[operationQ u];
[u release];
该操作本身创建一个新的 ManagedObjectContext 并尝试从 Web 下载一些 xml 并尝试更新 coredata 数据库...(此代码有效!)
在我的主控制器中,我收到上下文更改消息,并使用 mergeChangesFromContextDidSaveNotification 来合并和更新我的主对象上下文。所有结果控制器都使用这个主对象上下文。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationManagedObjectContextDidSave:) name:
NSManagedObjectContextDidSaveNotification object:nil];
实际上一切正常,但是当我在 NSOperation 中插入一个新对象时,UI 需要 4-6 秒才能更新并显示这个新对象...此外,UI 会阻塞...不可能滚动或触发其他交互.. 。
当我不使用队列并且我将代码下载并更新对象到我的 uitableviewcontroller 类中的方法中时,我使用这个
[NSThread detachNewThreadSelector:@selector(codeFromUpdatePersonsOperation) toTarget:self withObject:nil];
一切都运行良好,没有任何延迟或 UI 冻结......
有人可以向我解释这种行为吗?
Thanks