ios 單例模式 兩種寫法

創建一個單例很多辦法。我先列舉一個蘋果官方文檔中的寫法。

 

  1. static AccountManager *DefaultManager = nil;  
  2.    
  3. + (AccountManager *)defaultManager {  
  4.     if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init];  
  5.     return DefaultManager;  
  6. }  

當然,在iOS4之後有了另外一種寫法:

 

 

  1. + (AccountManager *)sharedManager  
  2. {  
  3.         static AccountManager *sharedAccountManagerInstance = nil;  
  4.         static dispatch_once_t predicate;  
  5.         dispatch_once(&predicate, ^{  
  6.                 sharedAccountManagerInstance = [[self alloc] init];   
  7.         });  
  8.     return sharedAccountManagerInstance;  
  9. }  

該寫法來自 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對象必須是全局或者靜態對象。這一點很重要,如果不能保證這一點,也就不能保證該方法只會被執行一次。)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章