NSFetchedResultsController 重复行
当使用辅助线程刷新 NSFetchedResultsController 内容时,我遇到了奇怪的行为,我想知道这是一个常见问题,或者我可能做错了什么。
我的主委托对象中有一个集中的 NSManagedObjectContext ,所有视图控制器都使用和共享该对象。通过执行获取并调用其委托方法来加载表后,将在后台启动辅助线程以更新其结果。然而,只有在奇怪的情况下,当插入新条目时,它们才会在表视图中重复。如果我退出并重新输入,重复的行就会消失,这使得它们仅存在于托管对象上下文中。
以下是以下步骤:
- 后台 NSOperation 线程创建一个链接到主应用程序委托的同一持久存储的受限上下文。
- 新线程开始监听 NSManagedObjectContextDidSaveNotification 通知。
- 新行被删除、更新或插入到辅助上下文中,从而在达到某个批量大小时始终进行保存调用。
在后台保存时,通知方法会在主线程上调用集中式上下文 mergeChangesFromContextDidSaveNotification 选择器,如下所示。
-(void)mergeChanges:(NSNotification *)通知 { NSManagedObjectContext *mainContext = [[appDelegate sharedDelegate] ManagedObjectContext]; [mainContext PerformSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:通知 等待完成:否]; }
完成操作后,侦听器将被删除,辅助上下文将被释放。
有谁知道导致我的表视图行重复的原因是什么,以及如何解决?
预先感谢您的帮助。
I'm experiencing a weird behavior when using a secondary thread to refresh NSFetchedResultsController contents and I'd like to know it this is a common issue or I might be doing something wrong.
I've got a centralized NSManagedObjectContext residing in my main delegate object which is used and shared by all view controllers. After loading a table by executing a fetch and calling its delegate method, a secondary thread is launched in background to update its results. However, and only in strange occasions, when inserting new entries they get duplicated in the table view. If I exit and reenter, duplicated rows disappear, what makes think they've only have existed in the managed object context.
These are the followed steps:
- A background NSOperation thread creates a confined context linked to the same persistent store of the main application delegate.
- The new thread starts listening NSManagedObjectContextDidSaveNotification notifications.
- New rows are deleted, updated or inserted into the secondary context making always a save call when reaching some batch size.
When saving in the background, the notification method calls the centralized context mergeChangesFromContextDidSaveNotification selector on the main thread as follow.
-(void)mergeChanges:(NSNotification *)notification { NSManagedObjectContext *mainContext = [[appDelegate sharedDelegate] managedObjectContext]; [mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:NO]; }
After finishing the operation the listener is removed and the secondary context released.
Does anybody have idea what is the reason is causing my table view rows to get duplicated, and how it can be solved?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,托管对象上下文中不能有“重复”对象,因为上下文中的每个对象都必须是唯一的。维护唯一的对象是上下文的核心功能,因此它不会发生。因此,您有两个条件之一:
我认为后者的可能性更大。
You can't actually have "duplicated" objects in a managed object context because every object in a context must be unique. Maintaining unique objects is the core function of a context so it just doesn't happen. So, you have one of two condtions:
I think the latter more likely.