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

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 创建单例的两种方法

时间: 2024-08-08 17:44:46

iOS单例写法简析的相关文章

iOS单例创建的一点疑惑

线程安全的单例常用写法, +(AccountManager *)sharedManager{ static AccountManager *defaultManager = nil; disptch_once_t once; disptch_once(&once,^{ defaultManager = [[self alloc] init]; }); return defaultManager; } 在用的过程中,有点疑惑的点是:static AccountManager *defaultMan

iOS 单例

iOS 单例 1.官方文档写法 static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } 2.来自 objcolumnist + (AccountManager *)sharedManager { s

单例写法 转

如何正确地写出单例模式 1.懒汉式,线程不安全 这段代码简单明了,而且使用了懒加载模式,但是却存在致命的问题.当有多个线程并行调用 getInstance() 的时候,就会创建多个实例.也就是说在多线程下不能正常工作 public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == nu

ios单例的实现

单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 这里主要介绍下在arc下,利用gcd实现单例. 第一步:声明一个静态实例 static SoundTool *_instance; 第二步:重写初始化方法 + (id)allocWithZone:(struct _NSZone *)zone 在对象初始化分配内存的时候都会调用这个方法,重写该方法时,即便用户没用通过shared方法获取实例,自己初始化依然可以保证得到的是同一个实例. 在gcd后,多线程下保证一个代码只被执

蓝懿 iOS iOS单例销毁

1 单例的实现见之前写过的一篇文章 iOS单例的快速实现 static InstanceClass *instance; + (InstanceClass *)defaultInstance{ @synchronized (self){ if (instance == nil) { instance = [[InstanceClass alloc] init]; } } return instance;} 2 单例的概念复习下 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而

iOS单例宏

先别急,先简单提一下单列的概念,当然具体的描述可能仁者见仁智者见智了! 1.单例设计模式(Singleton) 1> 什么是单列: 它可以保证某个类创建出来的对象永远只有1个 2> 作用(为什么要用) 节省内存开销 如果有一些数据, 整个程序中都用得上, 只需要使用同一份资源(保证大家访问的数据是相同的,一致的) 一般来说, 工具类设计为单例模式比较合适 3> 怎么实现,老程序员是碰到这样的问题的!arc下就少了! MRC(非ARC) ARC 废话少说,先来看一下我的单例模式下的头文件!

有一鲜为人知的单例写法-ThreadLocal

还有一鲜为人知的单例写法-ThreadLocal 源码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们平常用双重检查锁很不一样.而是用来一个ThreadLocal,这个也可以实现单例啊,那这个与双重检查锁实现的单例有什么区别呢? 1.FocusFinder /** * The algorithm used for finding the next focusable view in a given direction * from a view

Egret中的三种单例写法

1 普通的单例写法 class Single{ private static instance:Single; public static getInstance():Single{ if(this.instance == null){ this.instance = new Single(); } return this.instance; } public run(){ } } Single.getInstance().run(); 2 Module写法.仿照的Egret中Res资源类写法.

另一鲜为人知的单例写法-ThreadLocal

另一鲜为人知的单例写法-ThreadLocal 源代码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们寻经常使用双重检查锁非常不一样.而是用来一个ThreadLocal.这个也能够实现单例啊,那这个与双重检查锁实现的单例有什么差别呢? 1.FocusFinder /** * The algorithm used for finding the next focusable view in a given direction * from a v