0

I'm able to work with objects on a context and work with relationships. My registered objects are behaving correctly.

My store is added to the coordinator with the correct configuration (the configuration having 3 entities, one entity with the two other entities having a to-many relationship to the first entity).

Saving the store with either -many entity or with both -many entities works correctly. However, when I add the one- entity to the context, the object graph will not save.

It seems like it would be a common beginner's problem, but that means its also a difficult solution for beginners. I expect I'm not the first one to have run into this kind of trouble?

UPDATE: Thank you for the replies and pointers. It seems the main trouble is as subtly pointed out, that I am not handling errors properly. After looking at this post, Core Data Entity Relationship Does Not Save Between Launches , I have started improving with error handling. The localized description on this issue was 'ID required'. While I still don't know what that means, I can at least have the chance to figure it out, now.

Community
  • 1
  • 1

1 Answers1

1

To-Many relationships are represented as sets on the object. So child ->> parent implies that child.parent is represented as a set. Therefore, when you add a new parent object, you need to add that parent object to the set before you save the context. If you say self.parent = inserted_parent_object, I am not sure what would happen. If you are going the other way around it is easier. When you insert the parent object, just set the child (assuming you have it) and save the context. That will work unless it is a many to many relationship.

If there is an error, the code and the error message would be helpful in order to help you debug. This one has code: CoreData adding relationships to-many

Community
  • 1
  • 1
Aditya Nagrath
  • 421
  • 2
  • 8
  • Thank you for your reply. I had to edit my comment and remove the parent-child references. The relationships are one-to-many which I used to mistake for parent-child. I apologize for that. The problem seems to be either in the configuration (which I doubt) or in the 'options' part of addPersistentStoreWithType method. You are correct, I need to improve my error handling. Most of my errors are set to nil. – Mark Leavenworth Jul 21 '13 at 07:36
  • 1
    what happens? it crashes? those are weird places to have issues with saving a context. Try deleting the app from simulator or phone and running it again. – Aditya Nagrath Jul 21 '13 at 08:01
  • It doesn't crash, it simply doesn't save anything to the store. If I comment out the line pertaining to the third entity's instance, then it saves the other two managed objects normally. – Mark Leavenworth Jul 21 '13 at 12:44