2

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

Cullen SUN
  • 3,517
  • 3
  • 32
  • 33
  • Hi, I saw your comment on my question http://stackoverflow.com/questions/14010187/correct-way-of-updating-an-nstableview-in-a-core-data-app and thought I'd better let you know that the -setManagedObjectContext: only works once in an app's life-cycle. I tried running it twice in a row and it hung. – Todd Dec 27 '12 at 16:21
  • Thanks Todd. I did try and found it did not work for my case... I think I shall seek for Help from Apple... – Cullen SUN Dec 27 '12 at 16:49

4 Answers4

1

I've found that

[self.managedObjectContext reset];
[myArrayController fetch:self];

forces my NSTableView (with NSArrayController) to re-populate and display newly processed NSManagedObjects.

durron597
  • 31,968
  • 17
  • 99
  • 158
Todd
  • 1,770
  • 1
  • 17
  • 42
  • Ok, so just clutching at straws but I've also found that, in my case, I was dropping down an NSPanel as a sheet over the table while it was importing objects. It seems to have caused untold havoc in my app. I resorted to displaying a progress bar in another portion of my main window, well away from the table. This seems to have a)stopped some random assertions and b)caused the array controller to update and even(!) respect the sort descriptor. – Todd Jan 03 '13 at 11:05
  • excuse me, but who is 'self' here? the NSTableView? the NSArrayController? some subclass of your NSWindowController? – Motti Shneor Nov 26 '20 at 20:14
1

If you see the latest data in the managed object context, but not in the array controller, you want:

[_yourManagedObjectContext processPendingChanges];
[_yourArrayController fetchWithRequest:nil merge:YES error:&error];
[_yourArrayController rearrangeObjects];

I use this in a Mac/iOS iCloud app to update the Mac app's data when the iCloud store changes.

Adam
  • 741
  • 6
  • 4
  • thanks for your sharing. However, I soloved the problem by getting support from Apple eventually... Please see my answers above. Thanks – Cullen SUN Mar 27 '13 at 03:06
  • You may be glad to hear that your answer (and NOT the one from Apple) resolved the problem in my case... For some reason, my (Entity based) NSArrayController would throw exceptions and won't read data until I "fethWithRequest:nil merge:error:" the "fetch:self" does nothing in my case - and I wonder why. – Motti Shneor Nov 28 '20 at 18:58
1

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];
}];
}
Cullen SUN
  • 3,517
  • 3
  • 32
  • 33
0

in case someone else finds this... in my case, I was building the array from user interaction (not Core Data), and then trying to show the tableView when they were done (on the same window)... and of course... seeing nothing!

[arrayController rearrangeObjects];

just before I wanted to show the tableView fixed it for me.

durron597
  • 31,968
  • 17
  • 99
  • 158