Frage

My application has a data set with library-like hierarchy: there are pages within chapters and chapters within an entire book and books within a library. Therefore I organized my data model so that the singleton library entity has a to-many relation to the books entity (and conversely); the books entity has a to-many relation to the chapters entity, and the chapters entity has a to-many relation to the pages entity. Each entity is bound to an array controller whose contents are represented in a table view that is bound to its array controller, so there is an array controller for each 'to-many' data item. The user of the application can amend the contents of this data by interacting with a window-console that has a a table view representing each of the data hierarchy: a books table view, a chapters table view, and a pages table view. So if, for example, the user wishes to add or delete a chapter entry to a book, they select the book in the window and then use an adjacent add or delete button (bound to the chapter array controller) to change the chapter data for the book. Everything works fine except when the user makes a change and then saves. Apparently when a save is made to the managed object context, the change does not occur across the hierarchy of the data--only the specific 'to-many' data item. For example, if the user deletes a chapter from the book, only the chapter entity data is deleted. The pages data remains intact (it's "orphaned") in the core data repository. Apparently I am not understanding how core data works with respect to 'to-many' data relations. What am I doing wrong in my design that prevents a save from having a cascading effect throughout my data hierarchy?

War es hilfreich?

Lösung

Core Data relationships have a "Delete Rule" which controls what happens if the source object of the relationship is deleted.

In your case, it makes sense to set the "Delete Rule" for the to-many relationships Book ->> Chapter ->> Page to "Cascade", so that, for example, deleting a book automatically deletes all related chapters, which in turn deletes all related pages. (You see why it is called "cascade"!)

The inverse (to-one) relationships Page -> Chapter -> Book should be set to "Nullify", so that, for example, deleting a chapter removes the chapter from the book.

For more information, see "Relationship Delete Rules" in the "Core Data Programming Guide".

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top