I have a window with a list full of Core Data objects, and a display window that opens when one is selected.
A certain action in the display window marks the Core Data objects as 'Read'.
To open an object, I pass the object to the display window, and also the sortDescriptors and filterPredicate that displayed the list, so the display window can navigate forward/backwards (the main window sometimes displays a subset of objects, which is why I need the sortDescriptors and filterPredicate).
Marking the opened object as read works perfectly. It updates in the main window automatically thanks to KVO.
However, when I navigate forward/back to a different object in the display window, marking as read no longer works. I walk through the code and see the code execute, check the new value, and it is marked as read - but this doesn't reflect in the main window, or in the database.
Is there anything obvious that could be causing this? I am not, at any point, creating a copy of the core data object. The display window uses an NSArrayController to fetch the data, and the sortDescriptors and filterPredicate are applied to it. My code to get the next object looks like this:
-(MyObject *object)nextObject {
// _object is the object being displayed by the display window.
// _listContainingObject is an NSArrayController set to Entity: Object
NSArray *list = [_listContainingObject arrangedObjects];
NSUInteger positionInList = 0;
for (MyObject *object in list) {
if ([[[object objectID] URIRepresentation] isEqualTo: [[_object objectID] URIRepresentation]]) { break;
}
positionInList++;
}
if (positionInList == [list count] - 1) return nil;
if (positionInList + 1 > [list count] - 1) return nil;
MyObject *object = [list objectAtIndex: positionInList + 1];
return object;
}
It really seems like I'm encountering some hidden gotcha of Core Data, like objects pulled out of a different NSArrayController are copies... but everything is running off the same ManagedObjectContext. The NSArrayController's managed object context is bound to a property of my app delegate that returns the same context everything else uses. I am scratching my head pretty hard on this one.