Note that there are some explanatory texts on larger screens.

plurals
  1. POFetchedResultsController doesn't see the changes in managedObjectContext after data import
    primarykey
    data
    text
    <p>I'm working on the data import part in my app, and to make the UI more reliable, i followed this Marcus Zarra article <a href="http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/" rel="nofollow">http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/</a></p> <p>The idea is that you make the import in a separate context in the background tread(i use GCD for that), and your fetchedResultsController's context merges the changes by observing the <strong>NSManagedObjectContextDidSaveNotification</strong>.</p> <p>The issue i get is very strange to me - my fetchedResultsController doesn't get those changes itsef and doesn't reload the TableView when the new data comes. But if i fire the following method, which makes the fetch and reloads the table - it gets it all there.</p> <pre><code>- (void)updateUI { NSError *error; if (![[self fetchedResultsController] performFetch:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } [self.tableView reloadData]; } </code></pre> <p>So now i call that method when i get the <strong>NSManagedObjectContextDidSaveNotification</strong> to make it work, but it looks strange and nasty to me.</p> <pre><code> - (void)contextChanged:(NSNotification*)notification { if ([notification object] == [self managedObjectContext]) return; if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(contextChanged:) withObject:notification waitUntilDone:NO]; return; } [[self managedObjectContext] mergeChangesFromContextDidSaveNotification:notification]; //TODO:Make it work as it should - merge, without updateUI [self updateUI];//!!!Want to get rid of this! } </code></pre> <p>Why can it be like this? Here is the code that is responsible for parsing the data and adding the Observer.</p> <pre><code>- (void)parseWordsFromServer:(NSNotification *)notification { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) , ^{ NSDictionary *listInJSON = [notification userInfo]; wordsNumbers = [[listInJSON valueForKey:@"words"]mutableCopy]; if ([wordsNumbers count]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil]; </code></pre> <p>//New Context for the new thread NSManagedObjectContext *backContext = [[AppDelegate sharedAppDelegate]backManagedObjectContext];</p> <pre><code> //Get all the words we already have on this device NSArray *wordsWeHave = [Word wordsWithNumbers:wordsNumbers inManagedContext:backContext]; //Add them to this list for (Word *word in wordsWeHave) [[List listWithID:[currentList listID] inManagedObjectContext:backContext]addWordsObject:word]; [backContext save:nil];!//Save the context - get the notification } }); } </code></pre> <p><strong>EDIT</strong> I use the <code>NSFetchedResutsControllerDelegate</code>, indeed, how else could i pretend my tableview to be updated if i didn't?</p> <p><strong>UPDATE</strong> Decided just to move to Parent - Child paradigm</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload