2011-10-28 53 views
3

我讀到GCD的文章,並有一個例子:Autoreleasepool和dispatch_async

dispatch_queue_t bgQueue = myQueue; 
dispatch_async(dispatch_get_main_queue(), ^{ 
    NSString *stringValue = [[[textField stringValue] copy] autorelease]; 
    dispatch_async(bgQueue, ^{ 
     // use stringValue in the background now 
    }); 
}); 

如果我把在單擊處理程序(將在autoreleasepool調用)方法,將我虧stringValue的,因爲點擊事件後,autoreleasepool會被銷燬?

回答

9

在那個內部塊?不,你不會失去那個價值。如果在塊內引用了Objective-C對象變量(尚未聲明爲__block)並且該塊被複制,則會自動保留該對象。當該塊被釋放時,該對象也將被釋放。 dispatch_async()負責複製和釋放該塊。

+0

現在有道理,謝謝 – INs