O'Reilly logo

Core Data in Swift by Marcus S. Zarra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asynchronous Saving

One of the biggest issues with threading and Core Data has to do with thread blocking. No matter how cleverly you write the import and export operations, sooner or later you need to block the main queue to let the main NSManagedObjectContext talk to the NSPersistentStoreCoordinator and save changes out to disk.

Fortunately, this is a solvable problem and requires a small change to the Core Data stack. If you start your Core data stack with a private queue, NSManagedObjectContext, and associate it with the NSPersistentStoreCoordinator, you can have the main NSManagedObjectContext as a child of the private NSManagedObjectContext. Furthermore, when the main NSManagedObjectContext is saved, it won’t produce a disk hit and will ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required