NSManagedObjectContext(): `init()` was deprecated in iOS 9.0: Use -initWithConcurrencyType

Suragch picture Suragch · Aug 18, 2015 · Viewed 10.1k times · Source

I was working through Core Data Stack in Swift - Demystified but when I got to the line

self.context = NSManagedObjectContext()

I got the warning

`init()` was deprecated in iOS 9.0: Use -initWithConcurrencyType: instead

I see that I can do one of the following for self.context =

NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.ConfinementConcurrencyType)
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)

but since ConfinementConcurrencyType is also deprecated now that leaves me MainQueueConcurrencyType and PrivateQueueConcurrencyType. What is the difference between these two and how should I choose which one to use? I read this documentation, but I didn't really understand.

Answer

DBoyer picture DBoyer · Aug 18, 2015

You essentially will always have at least 1 context with NSMainQueueConcurrencyType and many contexts with NSPrivateQueueConcurrencyType. NSPrivateQueueConcurrencyType is used typically for saving or fetching things to core data in the background (like if attempting to sync records with a Web Service).

The NSMainQueueConcurrencyType creates a context associated with the main queue which is perfect for use with NSFetchedResultsController.

The default core data stack uses a single context with NSMainQueueConcurrencyType, but you can create a much better app by leveraging multiple NSPrivateQueueConcurrencyType to do any work that does not affect the UI.