0
votes

I have two entities in database, Category and Subcategory. Category can have multiple subcategories. The relationships are defined like this:

Category->Subcategories(to-many, has inverse, cascade)

Subcategory->Category(to-one, has inverse, nullify)

It all works ok but sometimes Category->Subcategories relationships is empty and Subcategory->Category is ok, which is weird. This happens very very rarely and I can't figure out the cause.

Below is a log from console in which it can be seen that instance of category has empty Subcategories relationship but one of subcategories in database has that same Category instance as its relationship.

I don't understand how one end of the relationship can be empty and other ok since they are automatically connected when either end is set.


Category: 0x7b6f8020> (entity: Category; id: 0x7b6f6d40 x-coredata://021C3D9A-7A0C-4326-B526-9E9253611C44/Category/p8> ; data: {
    … other data
    subcategories =     (
    );
})

Subcategory: 0x7c8f7770> (entity: Subcategory; id: 0x7c8c91b0 x-coredata://021C3D9A-7A0C-4326-B526-9E9253611C44/Subcategory/p5> ; data: {
    … other data
    category = "0x7b6f6d40 x-coredata://021C3D9A-7A0C-4326-B526-9E9253611C44/Category/p8>";
})
1
weird. maybe add screenshots of data model in XCode so we can help look for something not set right?RobP
Are these two objects from the same managed object context?Tom Harrington
Yes, these two objects are from the same MOC. Data model is ok, otherwise it wouldn't work at all. I found a piece of code that changes some attributes in Subcategory entity outside of 'performblock', could that be the cause? This happened two times in a month so its very hard to debug, last time it happened I saved a database file but still cant figure it out.El Horrible

1 Answers

0
votes

Do you use refreshObject:mergeChanges:? The documentation seems to suggest that this can lead to problems such as yours:

You can turn a realized object into a fault with the refreshObject:mergeChanges: method. If you pass NO as the mergeChanges argument, you must be sure that there are no changes to that object’s relationships. If there are, and you then save the context, you will introduce referential integrity problems to the persistent store.

See the Apple documentation here.