First of all, I tried to use the FetchedResultsController to solve my problem. This was an atypical use of FRC in that I did not update the TableView, I used it only to determine if entities were changing, so I knew when to save to the server:
self.fetchedResultsController = [Document MR_fetchAllSortedBy:@"timestamp" ascending:NO withPredicate:[NSPredicate predicateWithFormat:@"report.remoteId != nil && dirty == YES"] groupBy:nil delegate:self inContext:_managedObjectContext];
The problem is that FRC does not receive updates when the relationship object changes. IE, if report.remoteId goes from zero to non-nil, I will not see the update, since FRC only listens for changes to the Document object. The restriction is indicated here: http://www.mlsite.net/blog/?p=825 and here Changing the property of managed objects does not cause NSFetchedResultsController to update the table view
Not sure if I really want to implement a workaround, as I fell as if I were using FRC for things that he was not going to do. I don’t think the apple will fix this restriction, so I really don’t want to break the circular snap into a square hole to solve my problem.
A couple of options
Leave a notification in the code after I saved the objects, and then listen to it elsewhere. The only thing I don’t like about this is that the programmer needs it to do this and keep it in the know, i.e. If someone comes in and saves the object elsewhere, he must remember to trigger a notification.
OR
Listen to saving MOC by default. This is what I really would like to do. Something like that:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDataModelChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:[NSManagedObjectContext MR_defaultContext]]; - (void)handleDataModelChange:(NSNotification *)note { NSSet *updatedObjects = [[note userInfo] objectForKey:NSUpdatedObjectsKey]; NSSet *deletedObjects = [[note userInfo] objectForKey:NSDeletedObjectsKey]; NSSet *insertedObjects = [[note userInfo] objectForKey:NSInsertedObjectsKey];
Here is my dilemma with this. This will listen for all changes in the MOC by default. I really only care about changes in a few entities. So yes, I can filter out the objects that excite me in every call. BUT, I have a case where I save a lot of other objects in which I do not care. This means that NSManagedObjectContextObjectsDidChangeNotification will fire a ton, and most of the time I don't care. I do not want to slow down my application by constantly receiving this notification and taking time to filter out all the objects that do not bother me.
Is there a way to listen to certain objects? Either in CoreData or in MagicalRecord?
If the answer is no, is there a good alternative for listening to changes to a specific entity (and its relationships)?