Cocoa NSManagedObjectContext():`init()`在iOS 9.0中被弃用:使用-initWithConcurrencyType
我一直在努力,但当我到达终点时Cocoa NSManagedObjectContext():`init()`在iOS 9.0中被弃用:使用-initWithConcurrencyType,cocoa,core-data,nsmanagedobjectcontext,Cocoa,Core Data,Nsmanagedobjectcontext,我一直在努力,但当我到达终点时 self.context = NSManagedObjectContext() 我收到了警告 `init()` was deprecated in iOS 9.0: Use -initWithConcurrencyType: instead 我发现我可以为self.context= NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.ConfinementC
self.context = NSManagedObjectContext()
我收到了警告
`init()` was deprecated in iOS 9.0: Use -initWithConcurrencyType: instead
我发现我可以为self.context=
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.ConfinementConcurrencyType)
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
但是由于
ConstraintConcurrencyType
现在也被弃用,这就剩下了我MainQueueConcurrencyType
和PrivateQueueConcurrencyType
。这两者之间有什么区别?我应该如何选择使用哪一种?我读了,但我没有真正理解。基本上,您将始终至少有一个NSMainQueueConcurrencyType
的上下文和许多NSPrivateQueueConcurrencyType
的上下文NSPrivateQueueConcurrencyType
通常用于在后台将内容保存或获取到核心数据(就像尝试将记录与Web服务同步一样)
NSMainQueueConcurrencyType
创建与主队列关联的上下文,该上下文非常适合与NSFetchedResultsController
一起使用
默认的核心数据堆栈使用单个上下文和
NSMainQueueConcurrencyType
,但您可以通过利用多个NSPrivateQueueConcurrencyType
来创建更好的应用程序,以执行任何不影响UI的工作。将这两个函数替换为以下函数:
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()
}
}
}
也许对你有帮助。谢谢你的回答。我是否可以对所有上下文使用
PrivateQueueConcurrencyType
?我假设它可能会导致奇怪的行为,也可能会导致崩溃。对于必须在主队列上执行的任何工作,不应使用PrivateQueueConcurrencyType
。您应该始终将MainQueueConcurrencyType
与NSFetchedResultsController一起使用。PrivateQueueConcurrencyType
使用后台线程,所以请记住这一点。