I feel like I've read every CoreData question on Stack Overflow today, and am still very very stuck. :)
I'm working on a application that uses CoreData that is based on the methods illustrated in the Stanford University cs193p Lecture 14 (the Photomania app). It uses a UITableViewController subclass that implements the NSFetchedResultsController delegates, and of course the table updates automatically as results are fetched.
Everything works but the UI blocks when the Document is populated with data, because it occurs in the Main thread (which is the document's managedObjectContext). I'm already downloading the data in a background thread, this is just the code that actually populates the NSManagedObjects that is causing the blocking. The lecture alludes to using the NSManagedObjectContext's Parent context in order to load up the Document in the background and then "refetch" the data in the main thread to populate the table. I almost have things working (I think) except I often get double entries in my table. It seems like the sort of thing [self.tableView beginUpdates] / [self.tableView endUpdates] would resolve, but because I'm doing the NSManagedObjectContext save in the background context I don't know where I would put it.
I may also be going about this the entirely wrong way. :) In any event, here is the relevant code:
NSManagedObjectContext *backgroundContext;
backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
// document is my UIManagedDocument
backgroundContext.parentContext = document.managedObjectContext;
[backgroundContext performBlockAndWait:^{
// Do stuff here to populate the document.
[backgroundContext save:nil];
}];