NSFetchedResultsController返回重复项(相同的objectID和引用) [英] NSFetchedResultsController returns duplicates (the same objectID and reference)

查看:79
本文介绍了NSFetchedResultsController返回重复项(相同的objectID和引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSFetchedResultsController时遇到了奇怪的行为。它是iOS 10中的新功能。直截了当的意思是: fetchedObjects 包含重复的对象。

I'm experiencing a weird behaviour with NSFetchedResultsController. It is new in iOS 10. Straight to the point: fetchedObjects contains duplicate objects.

在我自己的标准中表示重复。它们在字面上具有相同的objectID和相同的引用。

This is not by any mean "duplicate" in my own criteria. They literally have the same objectIDs and the same reference.

在我的应用程序中如何发生:

How does it happen in my app:


  1. FRC已设置并执行获取

  1. FRC is setup and fetch performed

已获取2个对象(在此示例中)

2 objects are fetched (in this example)

在我的同步层中发生了什么,另一个对象(与获取项相关的对象)也被更新

Something happens in my sync layer, another object (that fetch items are in relationship with) is updated

FRC调用委托内容确实发生了变化。 fetchedObjects 包含4个对象(2个重复项)

FRC calls delegate that the content did change. fetchedObjects contains 4 objects (2 duplicates)

我在 controllerDidChangeContent 方法中进行调试的控制台输出。

Here's a console output from my debugging in controllerDidChangeContent method.

po frc.fetchedObjects

    - 0 : <ListItem: 0x1700dc3f0> (entity: ListItem; id: 0x1706294a0 <x-coredata://12D0CB00-7BF4-402A-8371-19DD1CFB1537/ListItem/t3384FC2D-3399-41FE-B7DD-C277922F495445> ; data: { ... }

    - 1 : <ListItem: 0x1700dc3f0> (entity: ListItem; id: 0x1706294a0 <x-coredata://12D0CB00-7BF4-402A-8371-19DD1CFB1537/ListItem/t3384FC2D-3399-41FE-B7DD-C277922F495445> ; data: { ... }

    - 2 : <ListItem: 0x1704c49f0> (entity: ListItem; id: 0x170631680 <x-coredata://12D0CB00-7BF4-402A-8371-19DD1CFB1537/ListItem/t3384FC2D-3399-41FE-B7DD-C277922F495446> ; data: { ... }

    - 3 : <ListItem: 0x1704c49f0> (entity: ListItem; id: 0x170631680 <x-coredata://12D0CB00-7BF4-402A-8371-19DD1CFB1537/ListItem/t3384FC2D-3399-41FE-B7DD-C277922F495446> ; data: { ... }


(lldb) po frc.fetchedObjects![0].objectID.isEqual(frc.fetchedObjects![1].objectID)
true

另一个说明:当我重新创建FRC并再次获取对象时,没有重复。

Another note: when I recreate FRC and fetch objects again, there are no duplicates.

我根本不知道是什么引起了这个问题。我的应用可能做错了导致该错误的错误。无论如何,FRC允许 fetchedObjects 中的重复对象确实很奇怪。
您能给我任何有关如何调试它的线索吗?

I simply have no idea what could be causing this problem. I might be doing something wrong in my app that is causing that bug. It's really weird anyway that FRC allows for duplicates objects in fetchedObjects. Can you please give me any clue of how should I debug it?

推荐答案

我遇到了类似的问题今天也。在我的情况下,我注意到FRC上提取的所有对象都有临时ID。可以这样验证它们:

I've come across similar problem today also. I noticed in my case the fetched objects on the FRC all had temporary IDs. These can be verified like so:

fetchedResultsController.fetchedObjects[0].objectID.isTemporary

要解决我的应用程序中的此问题,在保存上下文之前,我明确要求所有插入对象的永久对象ID

To get around this problem in my App I explicitly request permanent object IDs for all the inserted objects before saving the context on which new data is imported/updated/synced:

NSError *error;
[bgContext obtainPermanentIDsForObjects:bgContext.insertedObjects.allObjects error:&error];

这篇关于NSFetchedResultsController返回重复项(相同的objectID和引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆