infinitered/cdq — pull request #25 by scottmills (2014-05-21 19:53:02+00:00)
Title: Fix for relationship add/removes not being persisted

I was running into a problem where relationships were not being persisted reliably. Every thing looked good in the running app but it was not saving the relationships to sqlite. I finally found this in the docs.

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-CJBDBHCB
Under the "To-many relationships" section

>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!`

I am still new to cdq and core data so there is probably a better way to do this, but I added a spec and a fix that seems to be working for me.