2

By example:

I have an entity User, and an entity Device.

User have a To Many relationship toward Device, called devices.

Device has the inverse of this relationship called user.


Now I collect, persist a bulk of devices from e.g. a network service, hydrate them into an NSSet, then I bound them to a particular user, so I do:

NSSet *collectedDevices = [API getSomeDevices];
someUser.devices = collectedDevices;

Will Core Data populate the inverse user relationship for each Device for me? Does it observe the setters for relationships?


Background:

I'm aware of the Core Data setters for setting collections, but I want to avoid using them. I'm actually reconstructing Core Data entities from JSON representations with KVC without hardcoded attributes, relationships, just enumerating their entity descriptions, and set matching values.

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • Is the NSSet a set of NSManagedObjects? – k1th Jan 28 '14 at 18:26
  • @k1th Yes. I created them on a child context when fetching from server. – Geri Borbás Jan 28 '14 at 19:51
  • @MartinR Nope, hoping for an instant YES / NO answer. – Geri Borbás Jan 28 '14 at 19:52
  • You will need to try it out. I don't think the relationships will work cross-context. Usually Coredata will create the reverse relationship (by design) – it does for me in my simpler apps. There are some interesting pointers regarding @dynamic properties and overriding KVO: http://stackoverflow.com/questions/6685407/do-i-have-to-manually-set-a-core-data-objects-inverse-relationship-in-code-on-t http://stackoverflow.com/questions/6191160/core-data-inverse-relationship-not-being-set Why don't you do it in the same context? – k1th Jan 28 '14 at 20:41
  • @k1th There are cases when I don't want to persist results only inspect. Thats why I need a temporary context. – Geri Borbás Jan 29 '14 at 10:24
  • @k1th By the time I set an NSSet of NSManagedObjects all the object will exist in the same (persistent) context by design. So this is not a cross context issue. It won't even work that way, I've seen it crashing. – Geri Borbás Jan 29 '14 at 10:26

1 Answers1

6

Yes, Core Data will set the inverse relationships whether you use properties or KVC or the Core Data specific methods such as -setPrimitiveValue:forKey:.

However, when it sets that inverse can be slightly variable. It can set it immediately in some situations and in others it may wait until the end of the run loop to set the inverse. As long as all of the objects being related are created against the same NSManagedObjectContext then the referential integrity will be maintained by Core Data.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Thanks, thats far enough for me. – Geri Borbás Jan 29 '14 at 10:27
  • Just on more thing. Why even Core Data creates methods like `-(void)addDevices:(NSSet*) values;` if it works trough a simple property set as well? Actually this method made me skeptic about this whole thing. – Geri Borbás Jan 29 '14 at 10:29