標籤:
建立一個單例很多辦法。我先列舉一個蘋果官方文檔中的寫法。
[cpp] view plaincopy
- static AccountManager *DefaultManager = nil;
-
- + (AccountManager *)defaultManager {
- if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init];
- return DefaultManager;
- }
當然,在iOS4之後有了另外一種寫法:
[cpp] view plaincopy
- + (AccountManager *)sharedManager
- {
- static AccountManager *sharedAccountManagerInstance = nil;
- static dispatch_once_t predicate;
- dispatch_once(&predicate, ^{
- sharedAccountManagerInstance = [[self alloc] init];
- });
- return sharedAccountManagerInstance;
- }
該寫法來自 objcolumnist,文中提到,該寫法具有以下幾個特性:
1. 安全執行緒。
2. 滿足靜態分析器的要求。
3. 相容了ARC
然後我還有點好奇的是dispatch_once,這個函數,沒見過啊。
於是就到官方的文檔裡找找看,是怎麼說的。
下面是官方文檔介紹:
dispatch_once
Executes a block object once and only once for the lifetime of an application.
void dispatch_once(
dispatch_once_t *predicate,
dispatch_block_t block);
Parameters
predicate
A pointer to a dispatch_once_t structure that is used to test whether the block has completed or not.
block
The block object to execute once.
Discussion
This function is useful for initialization of global data (singletons) in an application. Always call this function before using or testing any variables that are initialized by the block.
If called simultaneously from multiple threads, this function waits synchronously until the block has completed.
The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage is undefined.
Availability
- Available in iOS 4.0 and later.
Declared In
dispatch/once.h
我們看到,該方法的作用就是執行且在整個程式的聲明周期中,僅執行一次某一個block對象。簡直就是為單例而生的嘛。而且,有些我們需要在程式開頭初始化的動作,如果為了保證其,僅執行一次,也可以放到這個dispatch_once來執行。
然後我們看到它需要一個斷言來確定這個代碼塊是否執行,這個斷言的指標要儲存起來,相對於第一種方法而言,還需要多儲存一個指標。
方法簡介中就說的很清楚了:對於在應用中建立一個初始化一個全域的資料對象(單例模式),這個函數很有用。
如果同時在多線程中調用它,這個函數將等待同步等待,直至該block調用結束。
這個斷言的指標必須要全域化的儲存,或者放在靜態區內。使用存放在自動分配地區或者動態地區的斷言,dispatch_once執行的結果是不可預知的。
總結:1.這個方法可以在建立單例或者某些初始化動作時使用,以保證其唯一性。2.該方法是安全執行緒的,所以請放心大膽的在子線程中使用。(前提是你的dispatch_once_t *predicate對象必須是全域或者靜態對象。這一點很重要,如果不能保證這一點,也就不能保證該方法只會被執行一次。)
iOS單例