I have a 1 to many relationship between 2 entities in my model. I fetch entity1 that is on the ‘1’ side and access the relationships set. When I find that there are no items currently in the set I request information from my service, create/insert entities, set properties, set relationship back to entity1, and save the ManagedObjectContext.
I then reload a TableView, which immediately accesses the relationships set on entity1 and checks the count to see if there are any relationships to display. No matter what I do, even if I call refreshObject, the count on the relationships set is always 0.
If I restart my app, the count is correct and I can see that the relationships to entity1 have been setup correctly.
Any idea what I am doing wrong here? I am doing similar code with other entities in my data model and am not having this problem. Something is strangely out of sync.
Thanks!
Are you sure you’re referencing the same entity1 object when you set the relationships? You can check by printing its URI representation when you fetch it in your table controller and when you do the import.
This may also be a result of using separate MOC instances to do the first fetch and the import (though that’s the correct way to do it). It might help if you give some more detail about your import routine.
Update:
(Answered in the comments below). Bidirectional relationships need to be established in both directions. For example: