码迷,mamicode.com
首页 > 移动开发 > 详细

iOS只执行一次的方法

时间:2015-11-11 17:57:15      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:

IOS开发(64)之GCD任务最多只执行一次

1 前言

使用 dispatch_once 函数 在 APP 的生命周期内来保证你想确保每段代码只执行一次,即使它在代码的不同地方多次调用(比如单例的初始化)。

 

2 代码实例
ZYAppDelegate.m

 

[plain]
/一个用于调度一次函数的标识
static dispatch_once_t onceToken;
//Block Object
void (^executedOnlyOnce)(void) = ^{
    static NSUInteger numberOfEntries = 0;
    numberOfEntries++;
    NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //声明一个队列
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //执行一次的队列
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

//一个用于调度一次函数的标识
static dispatch_once_t onceToken;
//Block Object
void (^executedOnlyOnce)(void) = ^{
    static NSUInteger numberOfEntries = 0;
    numberOfEntries++;
    NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //声明一个队列
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //执行一次的队列
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    dispatch_once(&onceToken, ^{ dispatch_async(concurrentQueue,
                                                executedOnlyOnce);
    });
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
ZYViewController.m

 

[plain]
- (void)viewDidLoad
{
    [super viewDidLoad];
    ZYMySingleton *test = [[ZYMySingleton alloc] init];
    //循环单例方法
    for (int i=0; i<5; i++) {
        [test sharedInstance];
    }
    [test release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    ZYMySingleton *test = [[ZYMySingleton alloc] init];
    //循环单例方法
    for (int i=0; i<5; i++) {
        [test sharedInstance];
    }
    [test release];
}
ZYMySingleton.m

 

[plain] view plaincopyprint?- (id) sharedInstance{
    static ZYMySingleton *SharedInstance = nil;
    //一个用于调度一次函数的标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SharedInstance = [ZYMySingleton new];
        NSLog(@"SharedInstance is ======>%@",SharedInstance);
    });
    return SharedInstance;
}

- (id) sharedInstance{
    static ZYMySingleton *SharedInstance = nil;
    //一个用于调度一次函数的标识
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SharedInstance = [ZYMySingleton new];
        NSLog(@"SharedInstance is ======>%@",SharedInstance);
    });
    return SharedInstance;
}
运行后控制台显示结果

iOS只执行一次的方法

标签:

原文地址:http://www.cnblogs.com/lidongxiao/p/4956631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!