NSPrivateQueueConcurrencyType serial or parallel?
As the name says, the question arises: if a NSManagedObjectContext
with concurrency type NSPrivateQueueConcurrencyType
is sequential or parallel.
In particular, if I call
[managedObjectContext performBlock:^{
}];
with long running time, will other calls in this context with executeBlock block until the first one completes?
source to share
I don't believe this is documented anyway. However, underlying data is generally not thread safe, and methods performBlock
and performBlockAndWait
are ways of dealing with this - by putting all of your underlying data in one queue. So I would be very surprised if it was a parallel queue, since the whole point is to avoid concurrency.
source to share
I can't find any evidence in the official docs, but I worked with recently NSPrivateQueueConcurrencyType
and I remember it was serial. Also this blog post states that:
When an NSManagedObjectContext is instantiated with initWithConcurrencyType: NSPrivateQueueConcurrencyType] or - [initWithConcurrencyType: NSMainQueueConcurrencyType], all access to the context and its managed objects must go through the method - [executeBlock: or - [executeBlockAndWait] Core Data uses a sequential queue to ensure that operations in the context are performed in order and that only one operation occurs at a time
source to share
This is a sequential queue from Apple Docs .
Or you can just try running this code and see the result. The numbers will be printed serially.
let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateMOC.perform {
for i in 0...8000 {
if i.isMultiple(of: 3000) {
print("1")
}
}
}
privateMOC.perform {
for i in 0...8000 {
if i.isMultiple(of: 3000) {
print("2")
}
}
}
source to share
This is a sequential queue. performBlock
the code will execute sequentially when called performBlock
and performBlockAndWait
if you specify NSPrivateQueueConcurrencyType
.
Apple doesn't pick a word at PrivateQueue
random, Private Queue
= Serial Queue
in Apple docs. See here for a description of Serial Queue
Sequential queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue.
Also, I double checked this in debug. Please see the latest thread in the screenshot below:
source to share