NSOperationQueue和NSFetchedResultsController

问题描述

| 我结合使用队列和resultscontroller来更新和显示一些coredata对象。 在我的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冻结... 有人可以向我解释这个行为吗? 谢谢     

解决方法

        另一个问题可能是更新UI需要在主线程上进行。我遇到了与您报告的问题相同的问题,最终发现如果您致电
[target performSelectorOnMainThread:@selector(methodToUpdateUI:) withObject:dataFromRetrievalOperation waitUntilDone:NO];
当线程完成处理后,这将导致UI立即更新。否则,它将等待大约5秒钟,然后UI动画才会发生。