an0
an0

Reputation: 17550

Serialization between NSOperationQueue and GCD

I know nowadays NSOperationQueue uses GCD. I want to confirm whether main operation queue and main dispatch queue are essentially the same queue, i.e., whether the execution order of block 1 before block 2 is guaranteed in code below:

dispatch_async(background_queue, ^{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // block 1
    }];        

    dispatch_async(dispatch_get_main_queue(), ^{
        // block 2
    });
});

Upvotes: 0

Views: 174

Answers (1)

bbum
bbum

Reputation: 162722

No, there is no guarantee that the two blocks will be executed in any given order. They might be, they might not. Doing so would require that NSOperationQueue enqueue the operation into the underlying GCD queue immediately. That would be counter to the general patterns of the class.

Any time you need execution of two operations to be serialized in relation to each other, you need to explicitly manage the concurrency in your code.

Upvotes: 2

Related Questions