small medium large xlarge

Back to: All Forums  Core Data
Md26_pragsmall
07 Jul 2011, 02:57
Mark Dorison (4 posts)

I posted this question on Stack Overflow (http://stackoverflow.com/questions/6565060/nsfetchedresultscontroller-not-displaying-changes-from-background-thread/6565117#6565117). I didn’t seem to be getting closer to the answer so I thought it couldn’t hurt to try here as well. Love the book.

My app is using an NSFetchedResultsController tied to a Core Data store and it has worked well so far, but I am now trying to make the update code asynchronous and I am having issues. I have created an NSOperation sub-class to do my updates in and am successfully adding this new object to an NSOperationQueue. The updates code is executing as I expect it to and I have verified this through debug logs and by examining the SQLite store after it runs.

The problem is that after my background operation completes, the new (or updated) items do not appear in my UITableView. Based on my limited understanding, I believe that I need to notify the main managedObjectContext that changes have occurred so that they may be merged in. My notification is firing, nut no new items appear in the tableview. If I stop the app and restart it, the objects appear in the tableview, leading me to believe that they are being inserted to the core data store successfully but are not being merged into the managedObjectContext being used on the main thread.

I have included a sample of my operation’s init, main and notification methods. Am I missing something important or maybe going about this in the wrong way? Any help would be greatly appreciated.

    - (id)initWithDelegate:(AppDelegate *)theDelegate
    {
        if (!(self = [super init])) return nil;
        delegate = theDelegate;
        return self;
    }

    - (void)main
    {
        [self setUpdateContext:[self managedObjectContext]];
        NSManagedObjectContext *mainMOC = [self newContextToMainStore];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self 
                   selector:@selector(contextDidSave:) 
                       name:NSManagedObjectContextDidSaveNotification 
                     object:updateContext];
        [self setMainContext:mainMOC];
                
        // Create/update objects with mainContext.
        
        NSError *error = nil;
        if (![[self mainContext] save:&error]) {
            DLog(@"Error saving event to CoreData store");
        }
        DLog(@"Core Data context saved");
    }


    - (void)contextDidSave:(NSNotification*)notification
    {
        DLog(@"Notification fired.");
        SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
        [[delegate managedObjectContext] performSelectorOnMainThread:selector
                                                          withObject:notification
                                                       waitUntilDone:YES]; 
    }

While debugging, I examined the notification object that is being sent in contextDidSave: and it seems to contain all of the items that were added (excerpt below). This continues to make me think that the inserts/updates are happening correctly but somehow the merge is not being fired.

    NSConcreteNotification 0x6b7b0b0 {name = NSManagingContextDidSaveChangesNotification; object = <NSManagedObjectContext: 0x5e8ab30>; userInfo = {
    inserted = "{(\n    <GCTeam: 0x6b77290> (entity: GCTeam; id: 0xdc5ea10 <x-coredata://F4091BAE-4B47-4F3A-A008-B6A35D7AB196/GCTeam/p1> ; data: {\n    changed = 
Md26_pragsmall
07 Jul 2011, 04:01
Mark Dorison (4 posts)

The answer turned out to be unrelated to the posted code which ended up working as I expected. For reasons that I am still not entirely sure of, it had something to do with the first launch of the app. When I attempted to run my update operation on launches after the Core Data store was created, it worked as expected. I solved the problem by pre-loading a version of the sqlite database in the app so that it did not need to create an empty store on first launch. I wish I understood why this solved the problem, but I was planning on doing this either way.

You must be logged in to comment