NSOperationQueue 和 NSFetchedResultsController
我使用队列和结果控制器的组合来更新和显示一些核心数据对象。
在我的 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];
操作本身创建一个新的托管对象上下文,并尝试从 Web 下载一些 xml 并尝试更新 coredata 数据库...(此代码有效!)
在我的主 Q 中控制器我收到上下文更改消息,并使用 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 冻结...
有人可以向我解释一下这种行为吗? ?
谢谢
i use a combination of queue and resultscontroller to update and display some coredata objects.
in my uitableviewcontroller i call every X second a method in my main controller object.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test:) userInfo:nil repeats:YES];
}
- (void)test:(NSTimer*)theTimer {
[[MainController instance] updatePersons];
}
In this method a custom NSOperation object will be added to my main Q.
- (BOOL)updatePersons {
UpdatePersonsOperation* u = [[UpdatePersonsOperation alloc] init];
[u setQueuePriority:NSOperationQueuePriorityVeryHigh];
[operationQ u];
[u release];
The operation itself creates a new managedobjectcontext and tries to download some xml from the web and tries to update the coredata database... (This code works!)
In my main controller i receive the context changed message and i use mergeChangesFromContextDidSaveNotification to merge and update my main object context. All resultscontroller use this main object context.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationManagedObjectContextDidSave:) name:
NSManagedObjectContextDidSaveNotification object:nil];
Actually everything works but when i insert a new object inside a NSOperation it takes 4-6 seconds till the UI updates and displays this new object... Furthermore the UI blocks... Its not possible to scroll or trigger a other interaction...
When i don't use a queue and i put my code to download and update the objects into a method inside my uitableviewcontroller class and i use this
[NSThread detachNewThreadSelector:@selector(codeFromUpdatePersonsOperation) toTarget:self withObject:nil];
Everything works very well without any delay or UI freeze...
Can someone explain me this behavoir?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一个问题可能是更新 UI 需要在主线程上进行。我遇到了与您报告的相同问题,最终发现如果您调用
This 将导致 UI 在线程完成处理后立即更新。否则,它会等待大约 5 秒,然后 UI 动画才会发生。
Another problem may have been that updating the UI needs to take place on the Main thread. I was experiencing the same issue that you reported, and eventually figured out that if you call
This will cause the UI to update immediately when the thread has finished processing. Otherwise, it waits about 5 seconds before the UI animations take place.