我结合使用queue和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数据库...(这段代码可以工作!)
在我的主控制器中,我接收到context changed消息,并使用mergeChangesFromContextDidSaveNotification合并和更新我的主对象上下文。所有的resultscontroller都使用这个主对象上下文。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationManagedObjectContextDidSave:) name:
NSManagedObjectContextDidSaveNotification object:nil];实际上,一切都正常,但是当我在NSOperation中插入一个新对象时,UI需要4-6秒才能更新并显示这个新对象。此外,UI阻塞...无法滚动或触发其他交互...
[NSThread detachNewThreadSelector:@selector(codeFromUpdatePersonsOperation) toTarget:self withObject:nil];一切运行良好,没有任何延迟或UI冻结……
有人能给我解释一下这种行为吗?
谢谢
发布于 2012-05-26 23:46:52
另一个问题可能是更新UI需要在主线程上进行。我遇到了你报告的同样的问题,最终我发现如果你打电话给
[target performSelectorOnMainThread:@selector(methodToUpdateUI:) withObject:dataFromRetrievalOperation waitUntilDone:NO];这将使UI在线程完成处理后立即更新。否则,它将等待大约5秒,然后UI动画才会发生。
https://stackoverflow.com/questions/6168361
复制相似问题