Core Data Inverse Relationship Not Being Set

前端 未结 3 506
情话喂你
情话喂你 2020-12-18 23:17

I\'ve got two Entities that I\'ll call A and B. They are configured with To-Many relationships in both directions, so A.myBs and B.myAs are both NSSets.

Here is

相关标签:
3条回答
  • 2020-12-18 23:49

    I just had the same problem, which was why I saw this question. Maybe my solution will help some people in the same situation.

    For me the answer was that in the NSManagedObject subclass I had over-riden:

    - (void)willChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects
    
    - (void)didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects
    

    This prevents the correct notifications being sent to the parent class (NSManagedObject) which handles the automatic reverse relationships.

    0 讨论(0)
  • 2020-12-19 00:08

    If you're subclassing the Managed Object, shouldn't there be auto-generated to-many relationship methods? So that you would just need to call:

    [aObject addBObject:bObject];
    
    0 讨论(0)
  • 2020-12-19 00:10

    I found the error, if you are overriding any of these methods

    // KVO change notification
    - (void)willChangeValueForKey:(NSString *)key;
    - (void)didChangeValueForKey:(NSString *)key;
    - (void)willChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects;
    - (void)didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects;
    

    it will cause NSManagedObject don't work well and the relationships won't be set correctly

    Apple documentation: You must not override these methods.

    0 讨论(0)
提交回复
热议问题