Slow deletes and saves in nested NSManagedObjectContext

Programming for ios: Slow deletes and saves in nested NSManagedObjectContext on newest questions tagged ios – Stack Overflow

In my app I have a “master” NSPrivateQueueConcurrencyType context which serves as a parent to a NSMainQueueConcurrencyType context that the view controller’s rely on and pass around. I wanted to do this to take advantage of async saves (this app uses iCloud with Core Data and saves do a lot of work exporting ubiquity logs). The setup is similar to the Zarra approach mentioned at the bottom of this article

Saves within the app typically look like:

[context save:nil];
[context.parentContext performBlock:^{
     [context.parentContext save:nil];

This seems to work well for small edits/updates, but I’m confused about what I see when I delete many objects (eg, delete a project that has hundreds of task objects related to it).

In that situation the save is async but it looks like the main thread gets into a semaphore wait situation (as seen when I pause the debugger) and is effectively blocked (I can’t scroll in the UI) until the background private context finishes the save.

Actually, I just noticed that swipe-to-delete deletions of a single object incur a noticeable delay, despite this async save pattern, so it seems all deletions in the app are slow.

If, alternatively, I alloc a new NSPrivateQueueConcurrencyType context, set its persistent store coordinator to the main PSC in the AppDelegate, and perform the delete and save, it still does a lot of work but the UI is never blocked (but then I have to worry about coordinating the contexts, refetching in the main context).

Any ideas what I might have done wrong, or have you seen this behavior also?

See Answers

Programming for ios: programming-for-ios

Leave a Reply