CoreData and to-many relationships.

I’ve had a problem with a category being able to reference many subjects but a subject only having one category. Kept getting a fault for the relationship. The error looked like this in the debugger:

CoreData: annotation: to-many relationship fault

This apparently is the solution, didn’t notice this in the main Core Data documentation:

——-
It is important to understand the difference between the values returned by the dot accessor and by mutableSetValueForKey:. mutableSetValueForKey: returns a mutable proxy object. If you mutate its contents, it will emit the appropriate key-value observing (KVO) change notifications for the relationship. The dot accessor simply returns a set. If you manipulate the set as shown in this code fragment:

[aDepartment.employees addObject:newEmployee]; // do not do this!
then KVO change notifications are not emitted and the inverse relationship is not updated correctly.

Recall that the dot simply invokes the accessor method, so for the same reasons:

[[aDepartment employees] addObject:newEmployee]; // do not do this, either!
——-

Hmm…. It would have been so much quicker for me to use SQLLite directly and issue my own SQL statements to manage the data as that’s what I’m used to. However I feel I should do it the ‘apple’ (e.g. slightly mental) way.

**Footnote: I’m not doing that. Ah well. I must be doing something like it. **

***Update:  It’s probably this, apple hides relationships from you under faults. If it didn’t it might follow all the relationships for one request and run out of memory. ***

This entry was posted in Programming. Bookmark the permalink.