我有此SWIFT代碼(從OBJ - C的遷移)夫特 - NSOperationQueue
NSOperationQueue().addOperationWithBlock({
print("WORKING...")
NSOperationQueue.mainQueue().addOperationWithBlock({
print("FINISHED")
})
})
的mainQueue塊不執行( 「已完成」 從不打印)。它出什麼問題了?
我有此SWIFT代碼(從OBJ - C的遷移)夫特 - NSOperationQueue
NSOperationQueue().addOperationWithBlock({
print("WORKING...")
NSOperationQueue.mainQueue().addOperationWithBlock({
print("FINISHED")
})
})
的mainQueue塊不執行( 「已完成」 從不打印)。它出什麼問題了?
這段代碼是完全正常的,執行的操作塊,但mainQueue
操作塊有時會花費時間執行,因爲mainQueue
將該塊添加到主線程的操作隊列,但不保證它何時執行。該隊列中可能還有其他項目仍在等待執行。
你說得對!另一個類中的另一個進程在「mainQueue」塊之前執行,因此它阻塞了另一個進程!謝謝! –
我使用GCD e.g:
dispatch_async(dispatch_get_main_queue()) {
[weak self] in
self.collection.reloadData()
}
請告訴我的Objective-C代碼。 – dasdom
'NSOperationQueue * opq = [NSOperationQueue new]; [opq addOperationWithBlock:^ {NSLog(@「WOKING ...」); [[NSOperationQueue mainQueue] addOperationWithBlock:^ {NSGL(@「FINISHED」); }]; }];' –
其實,你的代碼對我來說工作正常。這個被執行的地方在哪裏? –