Search code examples
macoscore-dataicloudcocoa-bindingsnsarraycontroller

How to force NSArrayController to reload MOC contents to reflect latest fresh data


I am working on a Mac & iOS App, with iCloud CoreData in between to synchronize the data.

When I update some thing from iOS App, and the updates are already migrated to the PersistentStore in Mac App while the Mac App is running. The problem is I cannot find an effective way to force the NSArrayController to reload all data from the store.

tried -(void) fetch:(id)sender; only can see the delete or added entity, but the updated model property not refreshed...

Please help. Thanks


Solution

  • Following is the reply from Apple's Developer Technical support. It worked for me. Thanks all for providing solutions.

    For the OS X app, when reloadFetchResults is called, you can ask the NSManagedObjectContext to reset itself and perform the fetch again.

    - (void)reloadFetchedResults:(NSNotification *)note
    {
    NSDictionary *userInfoDict = [note userInfo];
    
    NSManagedObjectContext *moc = self.coreDataController.mainThreadContext;
    
    // this only works if you used NSMainQueueConcurrencyType
    // otherwise use a dispatch_async back to the main thread yourself
    //
    [moc performBlock:^{
        [self mergeiCloudChanges:userInfoDict forContext:moc];
    
        [moc reset];
        [self.tableArrayController fetch:self];
    }];
    }