0

The app allows to manage a list of Suppliers and a list of Products available from those Suppliers. Data model contains 2 entities: Supplier and Product.

Supplier has a 1-to-many relation to Product, with delete rule set to Cascade (as I don't want to keep records of a supplier's products if it gets put out of business). Product has default relationship settings.

In IB, I have 2 array controllers. One points to the Supplier entity, with Parameters: MOC bound to AppDelegate and MKP=managedObjectContext. The other points to the Product entity, with Parameters: MOC bound to AppDelegate and MKP=managedObjectContext, and with Controller Content: Content Set bound to Suppliers array controller, CK=selection and MKP=name_of_relation.

On the interface I have 2 NSTableViews for Suppliers and Products, and buttons to Add/Remove from the tables. When I select a Supplier, only its specific Products are displayed on the Products table. When I delete a product, the product disappears from the table. It works as intended.

I thought it was working fine, until the day where I was curious to see how CoreData actually manages the data fields and tables inside the SQLite database. So I opened it using the SQLiteManager add-on in Firefox and... horror! I see that the Products are not deleted. The data are still there! Only the reference to the Supplier was deleted and that's why it doesn't appear anymore in the table, leading me to think that it had been correctly deleted.

What did I do wrong? Is it something with the Content Set bindings? Thanks for advice.

1 Answers1

0

I just ran a test on this and the result was that the relationship entities where completely deleted from the sqlite database. I think you need to check your cascading settings again exactly in IB. Did you make sure you have a save: statement?

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thank you for your reply. Are you referring to the save: method of the application? Yes, it's there and working. For example, when I delete a Supplier, it deletes in cascade all its related Products appropriately. I check using Firefox and the records are no longer there. I checked the settings in the data model, but what's so special I should check there? There aren't so many options... –  Aug 20 '11 at 13:39
  • Yes, I was referring to the `[managedObjectContext save:&error]` method. I know the model editor is very simple, just thought it was good to double check. - Well great, then it is working! Please consider accepting my answer. – Mundi Aug 20 '11 at 19:04
  • No it's not working ;-) When I delete a Supplier, all its Products are deleted. But when I delete a Product alone, it is not deleted for real. My problem is still there. I made a second application to test it, thinking that the first one may be bugged but still have the problem. I've read again Apple's doc about bindings and Core Data. Really can't see where the problem is, but there is definitely something I'm doing wrong here... –  Aug 22 '11 at 07:39
  • Just checked this on my sample test project. The row is deleted from the persistent store. Again, check if you `save:`. – Mundi Aug 28 '11 at 13:45
  • Mundi, sorry for my delayed reaction, I just noticed today your last reply. I added a line of code to *force* the save, but it still doesn't remove the record from the database. I inspected again the data model relations between the entities and nothing seems wrong. So to summarize the problem, the problem only occurs with child data in parent/child relationships. –  Sep 11 '11 at 14:25
  • When I delete the parent, it correctly deletes all its children (cascade) but when I delete 1 child independently from others, it is not deleted. Only the relationship is broken but the record is still present in the database. –  Sep 11 '11 at 14:29
  • Finally getting back to my project, I just found the solution on this post: [link](http://stackoverflow.com/questions/1598728/how-do-i-delete-an-entity-when-removing-it-from-an-array-controller). So it was indeed that simple: check the box "Deletes objects on remove" in the ArrayController inspector. Holy mackerel! ;) –  Jan 23 '12 at 12:01