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

  • 相关阅读:
    Redis常见数据类型二:Hash
    Redis常见数据类型一:String
    了解Docker
    微信小程序倒计时秒杀
    笛卡尔积求二维数组所有组合
    git好网站网址搜集
    npm i 报错Can't find Python executable "python2.7", you can set the PYTHON env variable
    css_注意小事项总结_随时更新
    echarts使用时报错cannot read property 'querycomponents' of undefined解决方案
    echarts中获取各个省份地图的链接
  • 原文地址:https://www.cnblogs.com/xuvw/p/3009702.html
Copyright © 2020-2023  润新知