`
ydbc
  • 浏览: 718551 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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


dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的,这就意味着你不需要使用诸如@synchronized之类的来防止使用多个线程或者队列时不同步的问题。


如果你要共享某个实例

+(MyInstance*)shareMyInstance()

static MyInstance *inst=nil;

static dispatch_once_t p;

dispatch_once(&p,^{

inst=[[MyInstance alloc]init];

});

return inst;


你任何时候访问共享实例,需要做的仅是:

MyInstance *myInst = [MyInstance shareMyInstance];
就这样,你在应用中就有一个共享的实例,该实例只会被创建一次。

分享到:
评论

相关推荐

    ios demo,dispatch_once,单例模式的应用

    ios demo,dispatch_once,单例模式的应用,ios demo,dispatch_once,单例模式的应用

    ios-Grand Central Dispatch的使用.zip

    通过这篇文章,你将学会dispatch_async、dispatch_after、dispatch_once、dispatch_apply、dispatch semaphore、dispatch group等。 详细介绍查看下面文章: github....

    详解iOS AFNetworking取消正在进行的网络请求

    简介 项目开发时,开发人员经常会遇到一种情况,A控制器push进入B控制器,B控制器正在进行网络请求,请求未结束时,点击返回回到A控制器... static dispatch_once_t once; dispatch_once(&once, ^{ httpManager = [

    学习iOS全局跑马灯

    static dispatch_once_t once; dispatch_once(&once, ^{ pModel = [[CCPaomaView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.0468 *KScreenHeight)]; }); return pModel; } 2.把接

    可添加视觉缩放效果

    static dispatch_once_t onceToken; dispatch_once(&onceToken;, ^{ motionmanager = [[CMMotionManager alloc] init]; }); return motionmanager; } 如果本来已经有UIImageView,那么可以进行扩展 #...

    iOS中设置网络超时时间+模拟的方法详解

    设置方法如下: 在封装的网络请求类里面如下设置 AFWEBAPI_REQUEST_TIMEOUT 这个参数为超时... static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] initWithBaseURL:[NSURL

    iOS实现代码只执行一次

    iOS实现代码只让执行一次 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. ... static dispatch_once_t hanwanjie; //只执行一次 disp

    Mac.OS.X和iOS中的并行开发

    This concise book shows you how to use Apple's Grand Central Dispatch (GCD) to simplify programming on multicore iOS devices and Mac OS X. Managing your application’s resources on more than one ...

    深入理解Swift中单例模式的替换及Swift 3.0单例模式的实现

    主要给大家介绍了关于Swift中单例模式替换的相关资料,然后又跟大家分享了关于Swift3.0 单例模式实现的几种方法-Dispatch_Once的内容,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

    IOS5 Programming Cookbook

    5.9 Performing a Task at Most Once with GCD 388 5.10 Grouping Tasks Together with GCD 390 5.11 Constructing Your Own Dispatch Queues with GCD 394 5.12 Running Tasks Synchronously with Operations 397 ...

    iphone h.264 live encode 实时 硬编码

    Once I have the parameters, I use a dispatch_source object to trigger reads from the file whenever new data is written. The body of the mdat chunk consists of H264 NALUs, each preceded by a length ...

    IOS Swift3 四种单例模式详解及实例

    Swift3 单例模式 常见的有这么几种方法 第一种简单到爆的 final class Single: NSObject { static let shared = Single() private override init()... public class func once(_ token: String, _ block: () -> Vo

    XMultiColumnListView

    // XListView need the scroll event, and it will dispatch the event to // user's listener (as a proxy). super.setOnScrollListener(this); // init header view mHeaderView = new XListViewHeader...

Global site tag (gtag.js) - Google Analytics