将NSManagedObject的关系更改为不同的上下文 [英] Change relationship of NSManagedObject to different context

查看:156
本文介绍了将NSManagedObject的关系更改为不同的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对先前问题的跟进:核心数据:以编程方式更改删除规则



我想重新整理我的问题,并在这里执行。



简单来说,我的应用程序允许从第三方数据库更新条目,但我想保留用户注释。所以我的工作流程是:


  1. 遍历所有实体

  2. 下载外部xml并将其解析


  3. 如果用户注释将其关系从旧实体更改为新实体
  4. ol>

    在导入期间,旧实体位于主上下文中,新实体位于临时导入上下文中。
    数字3给我的问题,如果我只是改变关系,然后他们不显示,如果我更新我的用户界面。如果我使用 objectID 获取注释,然后更改关系如下:

      NSManagedObjectID * objectId = oldAnnotation.objectID; 
    注释* newAnnotation = [importContext objectWithID:objectId];
    [newEntry addAnnotationObject:newAnnotation];

    它仍然无法正常工作 - 不显示。



    EDIT:如果我把第二行中的上下文改为 newEntry.managedObjectContext ,我得到一个非法尝试建立一个关系foo





    更新:经过一些深夜的头发调试,我发现我在获取newEntry时,我实际上是抓取oldEntry,因此没有任何更改会显示。



    使用上面的代码复制旧的注释,然后复制属性。对于一些用户输入本身的关系,我不得不做一个深度复制,我在这里发现:如何复制或复制Core Data Managed Object?

    解决方案

    我认为创建一个新实体并删除旧实体是一个有问题的策略。您应该尝试正确更新现有实体,并且只有在尚未存在的情况下才创建新实体。



    每当我需要来自不同上下文的对象时,我会提取。话虽如此,你的对象id代码应该工作。但是,可能会有各种其他毛刺,您应该检查:




    • 您保存importContext吗?
    • $ b
    • 是否将修改后的对象图保存到持久存储中?

    • 您是否在保存之后检查结果?


    This is a follow up to an earlier question: Core Data: change delete rule programmatically.

    I'd like to rephrase my question, and will do that here.

    Briefly, my app allows updating entries from a 3rd party database, but I'd like to keep user annotations. So my workflow is:

    1. iterate over all entities
    2. download external xml and parse it into a new entity
    3. if user annotations, change their relationship from old entity to new entity
    4. delete old entity

    During the import, the old entity is in the main context, the new entity is in a temporary import context. Number 3 gives me problems, if I just change the relationship, then they don't show if I update my UI. If I use the objectID to get the annotation and then change the relationship as follows:

        NSManagedObjectID *objectId = oldAnnotation.objectID;
        Annotation *newAnnotation = [importContext objectWithID: objectId];
        [newEntry addAnnotationObject: newAnnotation];
    

    It's still not working - it's not showing up.

    EDIT: if I change the context in the second line to newEntry.managedObjectContext, I get an Illegal attempt to establish a relationship 'foo' between objects in different contexts error.

    What am I missing?

    UPDATE: After some late-night hair-pulling debugging, I found that I when I was fetching the newEntry, I was actually fetching the oldEntry, therefore none of the changes would show up. The answer below by @Mundi pointed me in the right direction.

    Copying the old annotations worked using my code above, followed by copying the attributes. For some user input with relationships in itself, I had to do a "Deep Copy", which I found here: How can I duplicate, or copy a Core Data Managed Object?.

    解决方案

    I think creating a new entity and deleting the old one is a problematic strategy. You should try to properly update the existing entities and only create new ones if they do not yet exist.

    Whenever I need an object from a different context, I fetch it. That being said, your object id code should work. However, there could be all sorts of other glitches, that you should check:

    • Did you save the importContext?
    • Did you save its parent context, presumably the main context?
    • Was the modified object graph saved to the persistent store?
    • Are you checking the results after you have saved?

    这篇关于将NSManagedObject的关系更改为不同的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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