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

2

29

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.

Barbarity answered 18/8, 2015 at 5:37 Comment(1)
Maybe this thread helps you.Peahen
A
32

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.

Apogee answered 18/8, 2015 at 6:7 Comment(2)
Thanks for your answer. Can I use PrivateQueueConcurrencyType for all of contexts? I assume that it may cause odd behavior or may cause crash as well.Besides
You should not use PrivateQueueConcurrencyType for any work that must be done on the main queue. You should always use MainQueueConcurrencyType with an NSFetchedResultsController. A PrivateQueueConcurrencyType uses a background thread, so keep that tin mind.Apogee
G
8

Replace these two function with the following one:

lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
    }()

// MARK: - Core Data Saving support

func saveContext () {
    if managedObjectContext.hasChanges {
        do {
            try managedObjectContext.save()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            let nserror = error as NSError
            NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
            abort()
        }
    }
}
Guttle answered 29/9, 2015 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.