Chapter 6. Blocks and Grand Central Dispatch
<br />
Item 40: Avoid Retain Cycles Introduced by Blocks Referencing the Object Owning Them
<br />
這一條講避免因為使用block而出現保留環的問題。
保留環的出現是因為當block里引用了某個對象的實例變量的時候,這個對象也會被引用。這是一種不太容易被發現的情況。如果是直接引用控制器對象,控制器對象又引用block的話,就很容易發現。環中涉及三個以上對象的時候就容易混亂,我一般得通過畫圖分析。
以前只知道用weak關鍵字來打破保留環,這篇提供了一個別的思路,就是在所執行的任務結束時,適時地把不再用的對象釋放掉,這樣環里的一個箭頭就不存在了,整個環也就被打破了。例子給的比較長,這里只放一下打破環的部分:
[_networkFetcher startWithCompletionHandler:^(NSData *data) {
NSLog(@“Request for URL %@ finished”, _networkFetcher.url);
_fetchedData = data;
_networkFetcher = nil;
}];
這里是數據取得以后,fetcher就沒有用了,于是及時釋放掉。
還有一個例子:
- (void)p_requestCompleted {
if (_completionHandler) {
_completionHandler(_downloadedData);
}
self.completionHandler = nil;
}
這里直接釋放了block,也就是說如果block用完就沒有用了,所以不再引用它。
總之是有很多靈活的方法來打破保留環,可以具體問題具體分析。
<br />
Item 41: Prefer Dispatch Queues to Locks for Synchronization
<br />
這一節其實講的是dispatch queue的用法。
基本的同步與異步就不說了。這里有一個小細節:
- (void)setSomeString:(NSString *)someString {
dispatch_async(_syncQueue, ^{
_someString = someString;
});
}
這是一個setter,里面是異步實現的。文中提到這里可能會有性能問題,因為使用異步派發的話,block會被copy,這里有一個copy時間。所以需要權衡copy花的時間和同步派發所花的時間,如果前者更多,就沒必要使用異步派發。
文中還介紹了設計getter和setter的思想,獲取是可以并發的,而設置需要同步執行,也就是說設置一個變量的時候,不應該再有其他的讀寫操作。
具體實現是采用barrier,常見的有兩個函數:
- dispatch_barrier_async
- dispatch_barrier_sync
共同點:都是當隊列里正在執行的block執行完畢后,再開始執行這個操作,執行時不會有別的block同時執行,當這個操作執行完畢后別的操作才會再開始執行。
關于不同點,文檔里是這么說的:
dispatch_barrier_async: Calls to this function always return immediately after the block has been submitted and never wait for the block to be invoked.
dispatch_barrier_sync: Submits a barrier block to a dispatch queue for synchronous execution. Unlike dispatch_barrier_async, this function does not return until the barrier block has finished. Calling this function and targeting the current queue results in deadlock.
所以區別是在于是不是立即返回。但是dispatch_barrier_sync里提到的這個死鎖情況我不是特別明白。(好了我現在明白了!sync都是會有這種死鎖出現,阻塞了當前線程同時又在等當前線程的任務執行。)
文中的setter和getter的寫法:
_syncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- (NSString *) someString {
__block NSString *localSomeString;
dispatch_sync(_syncQueue, ^{
localSomeString = _someString;
});
return localSomeString;
}
- (void)setSomeString:(NSString *)someString {
dispatch_barrier_async(_syncQueue, ^{
_someString = someString;
});
}
<br />
Item 42: Prefer GCD to performSelector and Friends
<br />
這一節講performSelector的局限性。
performSelector確實挺少見的,除了在runtime講動態綁定的部分見過,平時比較少見,不知道它有多線程相關的功能。它的局限性在于返回值和參數,返回值是id,參數最多只有兩個,不夠靈活。并且在runtime動態綁定時,由于ARC不再根據方法名采用自動釋放,還有內存泄露的可能性存在。
所以采用GCD辦法來代替performSelector相關的方法。如果需要延后執行,應該選擇dispatch_after
,而不是performSelector:withObject:afterDelay:
。如果是主線程執行,應該選擇dispatch_get_main_queue()
作為dispatch queue,而不是調用performSelectorOnMainThread:withObject:waitUntilDone:
。