Chapter 7. The System Frameworks
<br />
Item 49: Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics
<br />
這一節講Toll-Free Bridging。
CFArrayRef aCFArray = (__bridge CFArrayRef)anNSArray;
這是一個NSArray橋接成CFArray的例子。非OC對象有ARC不適用的問題。bridged cast涉及到ARC對OC對象的引用,__bridge
和__bridge_transfer
(把CF數據結構變成OC對象)都是保留了ARC對OC對象的所有權,所以就不用手動寫對象的釋放了。而__bridge_retained
則是放棄了所有權,這時需要手動調用CFRelease(CFObject)來手動釋放非OC對象。
使用橋接來轉換的目的是使用CoreFoundation中具有的一些獨特功能。文中舉的例子是改變字典對鍵和值得內存語義。普通的OC字典,對鍵進行copy,對值進行retain,而用CFDictionary可以改變這一點。例子有點長,然后我好像不是特別懂…等我懂了再來更一下。
<br />
Item 50: Use NSCache Instead of NSDictionary for Caches
<br />
這一節講NSCache對象來做緩存。
抄一段Reference:
An NSCache object is a collection-like container, or cache, that stores key-value pairs, similar to the NSDictionary class.
While a key-value pair is in the cache, the cache maintains a strong reference to it. A common data type stored in NSCache objects is an object that implements the NSDiscardableContent protocol. Storing this type of object in a cache has benefits, because its content can be discarded when it is not needed anymore, thus saving memory.
NSCache objects differ from other mutable collections in a few ways:
- The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.
- You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.
- Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
其實這一節講的內容以上就大概都提到了。首先這個類是thread-safe的,因為緩存如果不thread-safe會很麻煩,我猜可能會引起重復下載。NSCache的鍵可以是不支持copy的對象,NSCache也不對鍵做copy操作,所以用起來比NSDictionary要靈活方便。另外比較厲害的是在內存緊張的時候它可以刪減使用頻率最低的對象。上面提到了這個對象需要實現NSDiscardableContent協議,比如NSPurgeableData類的對象。NSDiscardableContent協議中,beginContentAccess
和endContentAccess
這兩個方法用來表示當前對象是否處于可被丟棄的狀態。
<br />
Item 51: Keep initialize and load Implementations Lean
<br />
這一節講load與initialize兩個方法。
都是初始化的方法,但是有一些不同之處。load是程序啟動時就會調用的,所有用到的類都會調用,調用時整個程序阻塞,要等待所有類加載完畢才會響應,所以盡量不要在里面做操作。特別是不要做用到別的類的操作,因為load的階段屬于fragile state,各個類的加載順序不定,也不能確定哪個類加載完畢了,所以可能不能正常調用別的類的方法。另外load是不遵守類繼承規則的,如果本類沒有實現load方法,就不會調用了,不會像繼承系統一樣向父類尋找方法的實現。
initialize采取的是懶加載的方式,程序使用到這個類時才會調用,調用時線程阻塞,如果是主線程,會造成UI無響應,所以也是盡量不要做復雜的操作。initialize應只用來設置內部數據,如果設置其他類的數據,也會出現類似的可能沒有完全加載好的問題。initialize遵守類繼承規則,如果本類沒有實現會向父類查找。
對于不能在編譯器初始化的全局對象,可以放在initialize做。這個坑我踩過。因為NSString對象的初始化可以寫在外面,我以為其他的對象也可以,然而并不能[\再見]。應該在外面聲明,然后在initialize內部進行初始化,此時已經不是編譯期而是運行期了,對象可以正常初始化了。
<br />
Item 52: Remember that NSTimer Retains Its Target
<br />
這一節講NSTimer是怎么出現retain cycle的,以及怎樣去解決。
直接看一下代碼分析一下吧:
@interface EOCClass : NSObject
- (void)startPolling;
- (void)stopPolling;
@end
@implementation EOCClass {
NSTimer *_pollTimer;
}
- (id)init {
return [super init];
}
- (void)dealloc {
[_pollTimer invalidate];
}
- (void)stopPolling {
[_pollTimer invalidate];
[_pollTimer = nil];
}
- (void)startPolling {
__weak EOCClass *weakSelf = self;
_pollTimer = [NSTimer eoc_scheduledTimerWithTimeInterval:5.0
block:^{
EOCClass *strongSelf = weakSelf;
[strongSelf p_doPoll];
}
repeats:(BOOL)repeats];
}
- (void)p_doPoll {
//Poll the resource
}
@end
@interface NSTimer (EOCBlocksSupport)
+ (NSTimer *)eoc_scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats;
@end
@implementation NSTimer (EOCBlocksSupport)
+ (NSTimer *)eoc_scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats
{
return [self scheculedTimerWithTimeInterval:interval target:self selector:@selector(eoc_blockInvoke:) userInfo:[block copy] repeats:repeats];
}
+ (void)eoc_blockInvoke:(NSTimer *)timer {
void (^block)() = timer.userInfo;
if (block) {
block();
}
}
@end
再放一個EOCClass類原來的startPolling方法對比一下:
- (void)startPolling {
_pollTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 traget:self selector:@selector(p_doPoll) userInfo:nil repeats:(BOOL)repeats];
}
這里的場景是創建一個EOCClass類型的對象(起個名字叫EOCObject吧),然后調用它的startPolling方法。EOCObject對于pollTimer的保留很明顯能看到。如果采用之前的startPolling方法,pollTimer因為以EOCObject為target,所以也會保留它,這樣就形成了保留環。這個保留環打破的時機是pollTimer調用invalidate
的時候,也就是調用stopPolling
方法或者EOCObject被銷毀時,但此時由于保留環的存在,EOCObject很難被銷毀,那就只有采取當外部指向這個保留環的最后一個引用撤銷時,手動調用stopPolling
,操作性比較差。
解決的思路是,不使用target來調用p_doPoll
方法,而改用傳入block來實現,并且使用weakSelf避免了block中的保留環。這樣pollTimer不再有指向EOCObject的強引用,也就避免了內存泄露的發生。具體的方法是,給NSTimer添加一個分類,把block作為userInfo傳入,然后調用時在block里調用p_doPoll
就可以了。
終于有時間完結了這本書。開心O(∩_∩)O 撒花!