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


I have an error (yellow warning) on my model on line:

    var managedObjectContext = NSManagedObjectContext()

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

What is causing this? How can I fix this issue?

Change it to:

var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)

You can download Apple's document to see more details.

Specifies that the context will use the thread confinement pattern.
Available in iOS 3.0 and later.
Deprecated in iOS 9.0.

Or Command+Click "NSManagedObjectContext" direct to NSManagedObjectContext.h:

@available(iOS, introduced=3.0, deprecated=9.0, message="Use another NSManagedObjectContextConcurrencyType")
case ConfinementConcurrencyType

@available(iOS, introduced=3.0, deprecated=9.0, message="Use -initWithConcurrencyType: instead")
public convenience init()

So it seems NSManagedObjectContext() use "ConfinementConcurrencyType" to init.When Apple deprecated "ConfinementConcurrencyType" in iOS 9.0,for the sake of coherence,Apple did not change the behavior of init() method. So you'd better use another NSManagedObjectContextConcurrencyTypes( PrivateQueueConcurrencyType, MainQueueConcurrencyType) with another init method:

init(concurrencyType: NSManagedObjectContextConcurrencyType)