2014-03-04 85 views

回答

2

currentQueue return mainQueue如果你在主線程中調用它。 new是新的隊列與新主題

+0

然後'新'會在後臺運行? –

+1

@IdanMoshe,是的。 –

2

new是初始化方法,其從NSObject

文檔NSOperationQueue繼承說:

此方法的allocinit的組合。像alloc一樣,它初始化新對象的isa實例變量,以便它指向類數據結構。然後調用init方法來完成初始化過程。

這意味着調用+new創建NSOperationQueue


currentQueue回報你在其上調用該方法的隊列中的新實例。的currentQueue

NSOperationQueue文檔說:

啓動該操作或零如果隊列無法確定

操作隊列即如果方法調用+currentQueuemainQueue,它可以返回mainQueue

1

證明概念由@Cy-4AH編寫。跳這可以澄清一點。

-(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"); 
    } 

}