2014-06-24 38 views
0

我有此SWIFT代碼(從OBJ - C的遷移)夫特 - NSOperationQueue

NSOperationQueue().addOperationWithBlock({ 
    print("WORKING...") 
    NSOperationQueue.mainQueue().addOperationWithBlock({ 
     print("FINISHED") 
    }) 
}) 

的mainQueue塊不執行( 「已完成」 從不打印)。它出什麼問題了?

+0

請告訴我的Objective-C代碼。 – dasdom

+0

'NSOperationQueue * opq = [NSOperationQueue new]; [opq addOperationWithBlock:^ {NSLog(@「WOKING ...」); [[NSOperationQueue mainQueue] addOperationWithBlock:^ {NSGL(@「FINISHED」); }]; }];' –

+2

其實,你的代碼對我來說工作正常。這個被執行的地方在哪裏? –

回答

0

這段代碼是完全正常的,執行的操作塊,但mainQueue操作塊有時會花費時間執行,因爲mainQueue將該塊添加到主線程的操作隊列,但不保證它何時執行。該隊列中可能還有其他項目仍在等待執行。

+0

你說得對!另一個類中的另一個進程在「mainQueue」塊之前執行,因此它阻塞了另一個進程!謝謝! –

0

我使用GCD e.g:

dispatch_async(dispatch_get_main_queue()) { 
    [weak self] in 
    self.collection.reloadData() 
}