3
votes

I want to implement a UI-responsive downloading and parsing of a large data set, saving it with Core Data.

My setup:

I display the downloaded content in a custom view controller. I don't use a NSFetchedResultsController.

There are 3 MOCs:

  1. masterMOC (responsible for saving to disk, NSPrivateQueueConcurrencyType)
  2. mainMOC (used by UI, NSMainQueueConcurrencyType, a child of the masterMOC)
  3. backgroundMOC (responsible for the import from JSON, created in a separate thread, a child of the masterMOC)

I am importing in batches - every 50 items I perform the MOC saving in the following way:

NSError *error;
[backgroundMOC save:&error];
NSManagedObjectContext *masterMOC = backgroundMOC.parentContext; //set during initialization               
[masterMOC performBlock:^{
    NSError *parentContextError = nil;
    [masterMOC save:&parentContextError];
}];

I expect the changes in the mainMOC to be made after the masterMOC is saved. If I try to access some relationship of a random managed object while the masterMOC is saving (saving takes some time), the UI hangs until the saving is completed.

Question: how to avoid the UI freeze while the masterMOC is saving?

1
Rereading your question: Is the code snippet above performed on the main thread or in a separate one?ilmiacs
I don't see the advantage of using a block then. Why don't you put backroundMOC and masterMOC into the same thread?ilmiacs
What does take so long, anyway? Parsing or saving? Is your data just big or also complex? I guess the safest path should be atomizing the data sufficiently to make commits short enough.ilmiacs
I've implemented the approach described here: stackoverflow.com/questions/10542097/…. I do saving of the masterMOC in a block because it was initialized with the NSPrivateQueueConcurrencyTypeGytis

1 Answers

0
votes

Your problem probably is that the data store is blocking while you are writing to it. So, either make the data store non-blocking (this may or may not be possible in your case) or if not viable, make the accessor non-blocking. In the latter case the GUI will not hang, but it also will not update either until the result of the access comes back.