Custom setter for Core Data relationship to force inverse, reciprocal relationship
I want to create a custom setter for an NSManagedObject core data one-to-many relationship (Object.related), so that when object A is added to B.related, B is automatically added to A.related, and if A is removed from B.related, B is automatically removed from A.related. I have been able to create custom setters for basic properties (such as NSString and NSNumber properties) before, but this is causing me some headaches. When my function开发者_运维技巧 -(void)setRelated:(NSSet*)newSet
is called, it seems that the Self.related property is already set with the newSet list! What is the best practice here? I have also looked into watching this property with didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects
but have not been able to get it to work properly.
The inverse of a relationship is managed by CoreData (if you set it up in your model). If you try to do it manually you will end up with all kinds of trouble.
The inverse is a property on all relationships, you should be getting warnings if they are not set in the modeling tool.
精彩评论