Pergunta

I'm trying to figure out what is the difference between those 2.

Does it mean currentQueue cannot be mainQueue or it's a wrong assumption?

Foi útil?

Solução

currentQueue return mainQueue if you call it in main-thread. new is new queue with new threads

Outras dicas

new is initializer method which NSOperationQueue inherits from NSObject

Documentation says:

This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.

This means calling +new creates new instance of NSOperationQueue


currentQueue returns you the queue on which the method was called.

NSOperationQueue docs of currentQueue says:

The operation queue that started the operation or nil if the queue could not be determined

I.e. if method calling +currentQueue is mainQueue, it can return mainQueue

prove of concept write by @Cy-4AH. Hop this can clarify a bit.

-(void) viewDidLoad {
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^(void){
        //this block will executed in a separate thread (not the main thread)
        if ([NSOperationQueue currentQueue] == [NSOperationQueue mainQueue]) {
            NSLog(@"= in block");
        } else {
            NSLog(@"not = in block"); //This will be log
        }
    }];



    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue addOperation:blockOperation];

    //this block will executed in the main thread
    if ([NSOperationQueue currentQueue] == [NSOperationQueue mainQueue]) {
        NSLog(@"= outside of block"); //This will be log
    } else {
        NSLog(@"not = outside of block");
    }

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top