Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

One-to-many relationship in Core Data - how is the owner object updated?

I am making an application that has two entities in the data model, with one entity having a one-to-many relationship to another one, let's say a Parent can have many Children. The Parent entity has relationships to all its children (as an NSSet-type property automatically created in the generated class file), and every Child has a relationship to its one Parent (as a Parent-type property in the generated class).

The application works as follows, first the user can create a Parent with all its properties set except that Children is automatically initialised as nil as I don't set it explicitly. This object is saved to Core Data. Then the user can create a Parent's children, and when preparing the object before saving to Core Data, I pass its Parent object to its *parent property, and everything seems to work properly - I get a parent list displayed correctly, and each parent has a list of its children displayed correctly, no crashes, everything seems to persist properly. I only save the new Child in the context and don't touch the Parent.

The question - how does the Parent entity know that it gets a new Child, when it has already been saved to Core Data before? Is it because I have an inverse relationship set, Core Data automatically informs Parent that it has a new Child, and updates the Core Data store accordingly?

Thanks for any insight on this matter. Hope I was able to express my question in an understandable way, if not - I'd love to elaborate more. The thing is that I am not sure why my implementation is working and what is actually happening under the hood.

3 Answers

I recommend you to check the documentation :

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-CJBDBHCB

the answer is pretty well explain (See «Inverse relationship » and «Manipulating Relationships and Object Graph Integrity»)

Hope thats help you !

Well, there's this line in the documentation: "By referencing the managed object model, the framework automatically determines from the current state of the object graph which relationships must be established and which must be broken". Does it mean the same thing I did above? Is it enough to do it the way I described above?

Hello P M, have you got around your issue above? I have the same problem and i cant find enough resources to explain it. I tried to follow the link provided by Guillama Maka but its expired.