CoreData: “Dangling reference to an invalid object.” error

后端 未结 9 2053
余生分开走
余生分开走 2021-01-01 11:44

I\'m working on a Cocoa-Touch app, it uses CoreData and has some NSPersistentObject subclasses generated by the XCode model editor.

I\'ve noticed that recently, when

相关标签:
9条回答
  • 2021-01-01 12:48

    I had the same problem, finally I found the problem was that I was setting a relationship between two different managed object context.

    0 讨论(0)
  • 2021-01-01 12:49

    I ran into this issue, and the problem had to do with different (or rather one nil) managed object contexts for entities that had a relationship. In my case, I created a relationship between the entities when both had nil Managed Object Contexts, and then added one of the entities to a MOC, and assumed the other would be added to the MOC as well. I assumed this because of the first two comments to the top answer on this SO thread, but that ended up being wrong.

    So lesson learned: if you add an entity to a MOC, other entities that have relationships to it do not get dragged into the MOC along with it. You have to add them to the MOC also or Core Data will not be happy.

    0 讨论(0)
  • 2021-01-01 12:50

    Let's say you have a table "recipes" and a child table "ingredients". You then create a one-to-many relation from recipe's to ingredients and also create an inverse relationship (one-to-one) from ingredients to recipes. It makes sense to specify a delete rule of "cascade" from the recipes table because if you delete a recipe the ingredient should also be deleted. However, if you specify "no action" in the delete rule on the one-to-one relationship in ingredients you will get the dangling reference error when you try to delete an ingredient. Change the delete rule on the one-to-one relationship to "nullify" and this should correct the problem.

    0 讨论(0)
提交回复
热议问题