天天看點

iOS 建立單例的兩種方法

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

static AccountManager *DefaultManager = nil;
 
+ (AccountManager *)defaultManager {
    if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init];
    return DefaultManager;
}
           

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

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