NSFetchedResultsController 重复行

发布于 2024-11-14 23:38:18 字数 1006 浏览 7 评论 0原文

当使用辅助线程刷新 NSFetchedResultsController 内容时,我遇到了奇怪的行为,我想知道这是一个常见问题,或者我可能做错了什么。

我的主委托对象中有一个集中的 NSManagedObjectContext ,所有视图控制器都使用和共享该对象。通过执行获取并调用其委托方法来加载表后,将在后台启动辅助线程以更新其结果。然而,只有在奇怪的情况下,当插入新条目时,它们才会在表视图中重复。如果我退出并重新输入,重复的行就会消失,这使得它们仅存在于托管对象上下文中。

以下是以下步骤:

  1. 后台 NSOperation 线程创建一个链接到主应用程序委托的同一持久存储的受限上下文。
  2. 新线程开始监听 NSManagedObjectContextDidSaveNotification 通知。
  3. 新行被删除、更新或插入到辅助上下文中,从而在达到某个批量大小时始终进行保存调用。
  4. 在后台保存时,通知方法会在主线程上调用集中式上下文 mergeChangesFromContextDidSaveNotification 选择器,如下所示。

    -(void)mergeChanges:(NSNotification *)通知
    {
        NSManagedObjectContext *mainContext = [[appDelegate sharedDelegate] ManagedObjectContext];
    
        [mainContext PerformSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
                                      withObject:通知
                                   等待完成:否];
    }
    
  5. 完成操作后,侦听器将被删除,辅助上下文将被释放。

有谁知道导致我的表视图行重复的原因是什么,以及如何解决?

预先感谢您的帮助。

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:

  1. A background NSOperation thread creates a confined context linked to the same persistent store of the main application delegate.
  2. The new thread starts listening NSManagedObjectContextDidSaveNotification notifications.
  3. New rows are deleted, updated or inserted into the secondary context making always a save call when reaching some batch size.
  4. 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];
    }
    
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-11-21 23:38:18

实际上,托管对象上下文中不能有“重复”对象,因为上下文中的每个对象都必须是唯一的。维护唯一的对象是上下文的核心功能,因此它不会发生。因此,您有两个条件之一:

  1. 您正在创建两个或多个具有相同属性的托管对象,以便它们在排序到表视图中时一起显示。
  2. 在某些情况下,您的表视图数据源逻辑实际上是将来自同一托管对象的数据两次返回到表视图。这会造成上下文中存在重复的托管对象的错觉。

我认为后者的可能性更大。

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:

  1. You are creating two or more managed objects with the same attributes such that they show up together when sorted into a tableview.
  2. Your tableview datasource logic is actually returning to the tableview the data from the same managed object twice in some circumstances. This would create the illusion that there is a duplicate managed object in the context.

I think the latter more likely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文