2011-05-29 70 views
2

我使用队列和resultscontroller的组合来更新和显示一些coredata对象。NSOperationQueue和NSFetchedResultsController

在我的uitableviewcontroller我调用每X秒在我的主控制器对象的方法。

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test:)   userInfo:nil repeats:YES]; 

} 

- (void)test:(NSTimer*)theTimer { 

[[MainController instance] updatePersons]; 

} 

在这种方法中自定义的NSOperation对象将被添加到我的主要问题:

- (BOOL)updatePersons { 

UpdatePersonsOperation* u = [[UpdatePersonsOperation alloc] init]; 
[u setQueuePriority:NSOperationQueuePriorityVeryHigh]; 

[operationQ u]; 

[u release]; 

操作本身创建一个新的managedobjectcontext并试图从网上下载一些XML和尝试更新coredata数据库...(此代码工作!)

在我的主控制器我收到上下文更改的消息,我用mergeChangesFromContextDidSaveNotification合并和更新我的主要对象上下文。所有resultcontroller都使用这个主对象上下文。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationManagedObjectContextDidSave:) name: 
             NSManagedObjectContextDidSaveNotification object:nil]; 

其实一切正常,但是当我插入的NSOperation内一个新的对象时,它需要4-6秒钟,直到UI更新并显示这个新对象......此外,UI块......它不可能滚动或引发其他互动......

当我不使用队列,我把我的代码下载和更新我的UITableViewController类中的对象到一个方法,我用这个

[NSThread detachNewThreadSelector:@selector(codeFromUpdatePersonsOperation) toTarget:self withObject:nil]; 

一切工作得很好,没有任何延迟或UI冻结...

有人可以解释我这种行为吗?

谢谢

+0

经过长时间的研究,NSFetchedResultsController出现问题!当您更新和加载来自web的复杂数据时,我的提示从不使用NSFetchedResultsController。为uitableview实现自己的更新逻辑。 – user547064 2011-07-01 13:16:29

回答

0

另一个问题可能是更新UI需要发生在主线程上。我遇到同样的问题,你的报道,并最终想通了,如果你打电话

[target performSelectorOnMainThread:@selector(methodToUpdateUI:) withObject:dataFromRetrievalOperation waitUntilDone:NO]; 

这将导致UI更新立即当线程完成处理。否则,它会在用户界面动画发生之前等待约5秒钟。