CoreData:backgroundContext 和子上下文与 privateQueueConcurrencyType 的区别?
CoreData: difference between backgroundContext and child context with privateQueueConcurrencyType?
使用 CoreData,您可以要求容器使用 container.newBackgroundContext()
创建背景上下文。
这对于在后台使用核心数据而不影响 UI 主上下文很有用。
您还可以使用 privateQueueConcurrencyType
创建子上下文
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
在这种情况下,如果我没记错的话,上下文仍将在不同队列的后台执行。
那么这两者有什么区别,什么时候该用一个,什么时候用另一个?
你初始化一个上下文,即:
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
在引入 NSPersistentContainer. See also https://developer.apple.com/documentation/coredata/using_core_data_in_the_background 之前常用,他们声明:
Initializing and Configuring Contexts For both contexts, the
initialization of the NSManagedObjectContext instance is the same: let
moc = NSManagedObjectContext(concurrencyType:<#type#>) The parameter
being passed in as part of the initialization determines what type of
NSManagedObjectContext is returned. When you use the
NSPersistentContainer, you configure the viewContext property as a
main queue
(NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
context, and configure the contexts associated with
performBackgroundTask(_:) and newBackgroundContext() as a private
queue
(NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType).
newBackgroundContext() 的文档说明如下:
Invoking this method causes the persistent container to create and
return a new NSManagedObjectContext with the concurrencyType set to
NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType.
This new context will be associated with the
NSPersistentStoreCoordinator directly and is set to consume
NSManagedObjectContextDidSave broadcasts automatically.
newBackgroundContext()
与您提供的代码一样,还有一些我在上面引述中强调的额外内容。
在为 iOS 10+ 开发时,我建议使用 NSPersistentContainer
并在您想阻塞调用线程时使用 newBackgroundContext()
,如果您这样做,则使用 performBackgroundTask
当您想将工作分派到后台时,不想调用线程阻塞。
我在 iOS 10 之前没有使用过 CoreData,但我认为在使用 concurrenyType
初始化上下文时,您需要做更多的事情才能使上下文正常工作。 newBackgroundContext()
.
不再需要了
使用 CoreData,您可以要求容器使用 container.newBackgroundContext()
创建背景上下文。
这对于在后台使用核心数据而不影响 UI 主上下文很有用。
您还可以使用 privateQueueConcurrencyType
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
在这种情况下,如果我没记错的话,上下文仍将在不同队列的后台执行。
那么这两者有什么区别,什么时候该用一个,什么时候用另一个?
你初始化一个上下文,即:
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
在引入 NSPersistentContainer. See also https://developer.apple.com/documentation/coredata/using_core_data_in_the_background 之前常用,他们声明:
Initializing and Configuring Contexts For both contexts, the initialization of the NSManagedObjectContext instance is the same: let moc = NSManagedObjectContext(concurrencyType:<#type#>) The parameter being passed in as part of the initialization determines what type of NSManagedObjectContext is returned. When you use the NSPersistentContainer, you configure the viewContext property as a main queue (NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType) context, and configure the contexts associated with performBackgroundTask(_:) and newBackgroundContext() as a private queue (NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType).
newBackgroundContext() 的文档说明如下:
Invoking this method causes the persistent container to create and return a new NSManagedObjectContext with the concurrencyType set to NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType. This new context will be associated with the NSPersistentStoreCoordinator directly and is set to consume NSManagedObjectContextDidSave broadcasts automatically.
newBackgroundContext()
与您提供的代码一样,还有一些我在上面引述中强调的额外内容。
在为 iOS 10+ 开发时,我建议使用 NSPersistentContainer
并在您想阻塞调用线程时使用 newBackgroundContext()
,如果您这样做,则使用 performBackgroundTask
当您想将工作分派到后台时,不想调用线程阻塞。
我在 iOS 10 之前没有使用过 CoreData,但我认为在使用 concurrenyType
初始化上下文时,您需要做更多的事情才能使上下文正常工作。 newBackgroundContext()
.