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,单例模式的应用
通过这篇文章,你将学会dispatch_async、dispatch_after、dispatch_once、dispatch_apply、dispatch semaphore、dispatch group等。 详细介绍查看下面文章: github....
简介 项目开发时,开发人员经常会遇到一种情况,A控制器push进入B控制器,B控制器正在进行网络请求,请求未结束时,点击返回回到A控制器... static dispatch_once_t once; dispatch_once(&once, ^{ httpManager = [
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,那么可以进行扩展 #...
设置方法如下: 在封装的网络请求类里面如下设置 AFWEBAPI_REQUEST_TIMEOUT 这个参数为超时... static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] initWithBaseURL:[NSURL
iOS实现代码只让执行一次 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. ... static dispatch_once_t hanwanjie; //只执行一次 disp
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中单例模式替换的相关资料,然后又跟大家分享了关于Swift3.0 单例模式实现的几种方法-Dispatch_Once的内容,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
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 ...
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 ...
Swift3 单例模式 常见的有这么几种方法 第一种简单到爆的 final class Single: NSObject { static let shared = Single() private override init()... public class func once(_ token: String, _ block: () -> Vo
// 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...