Cocoa Mac app and NSTableView don't refresh with NSArrayController and NSManagedObjectContext

254 views Asked by At

I'm developing a new Core Data app and I created an NSArrayController (Entity Mode) to make bindings, it works very well but when the SQLite store changes (changes come from other clients using TICoreDataSync library) I force a tableView reload with no success. Here the code:

NSError *saveError = nil;
    [self.managedObjectContext save:&saveError];
    if (saveError != nil) {
        NSLog(@"%s %@", __PRETTY_FUNCTION__, saveError);
    }
[arrayController fetch:nil];
[tableView reloadData];

If I close the app and start it again I can see changes in my tableView but I would like it to refresh automatically. Thanks in advance.

2

There are 2 answers

1
Drew McCormack On

Are you calling mergeChangesFromContextDidSaveNotification: to update your context when TICDS makes changes?

You should implement this delegate method:

-(void)documentSyncManager:(TICDSDocumentSyncManager *)aSyncManager 
    didMakeChangesToObjectsInBackgroundContextAndSaveWithNotification:(NSNotification *)aNotification

Make sure you invoke mergeChangesFromContextDidSaveNotification: on the appropriate thread.

Also worth looking at a sync framework I am developing called Ensembles. TICDS is not being maintained very much these days — I've worked on it in the past — and Ensembles is quite a bit easier to use.

0
rsd_oilpoint On

I added the following line before fetch and it works:

[arrayController setManagedObjectContext:[[NSApp delegate] managedObjectContext]];

Good for me that it works but I don't understand why I need to set it everytime I start fetching, the managedObjectContext is set at startup or it wouldn't show values once the app started.