zoukankan      html  css  js  c++  java
  • 有关objc中的单例

    1 创建单例方法:

    以下来自:http://blog.sina.com.cn/s/blog_69081e0601019m1z.html

    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. }  


    该写法来自http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/,文中提到,该写法具有以下几个特性:

    1. 线程安全。

    2. 满足静态分析器的要求。

    3. 兼容了ARC

    官方文档介绍:

    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对象必须是全局或者静态对象。这一点很重要,如果不能保证这一点,也就不能保证该方法只会被执行一次。)

  • 相关阅读:
    centos安装nginx
    Vue练习十一:02_05_函数传参改变Div任意属性的值
    Vue练习十:02_04_弹出层
    Vue练习九:02_03_求数组中所有数字的和
    Vue练习八:02_02_点击div显示内容
    Vue练习七:02_01_百度输入法
    Vue练习六:01_06_记住密码提示框
    Vue练习五:01_05_鼠标移入改变样式鼠标移出恢复
    Vue练习四:01_04_点击将DIV变成红色
    Vue练习三:01_03_函数传参
  • 原文地址:https://www.cnblogs.com/xuvw/p/3009702.html
Copyright © 2011-2022 走看看