NSPrivateQueueConcurrencyType serial or concurrent?

Viewed 1029

As the title says, the question is, if a NSManagedObjectContext with concurrency type NSPrivateQueueConcurrencyType is serial or concurrent.

More specifically, if I call

[managedObjectContext performBlock:^{

}];

with a long running task, will other calls to that context with performBlock be blocked until the first one finished?

6 Answers

It is serial queue from Apple docs.

Or you can simply try to run 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")
            }
        }
    }

It's a serial queue. You code will be performed serially when you call performBlock and performBlockAndWait if you specify NSPrivateQueueConcurrencyType.

Apple doesn't pick the word PrivateQueue randomly, Private Queue = Serial Queue in Apple's documents. See here for the Serial Queue description

Serial 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 have double checked that in debugging. Please see the last thread in the below screenshot: enter image description here

I have tested that all tasks will be executed in sequence, but It may be executed in a different thread

 backgroundContext.perform {
    print("perform------:\(self.number)", Thread.current)
    self.number += 1
 }

enter image description here

Related