欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-10-27 13:51:58
1 前言 使用 dispatch_once 函数 在 app 的生命周期内来保证你想确保每段代码只执行一次,即使它在代码的不同地方多次调用(比如单例的初始化)。  ...

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;
}
运行后控制台显示结果


2013-05-10 17:30:49.334 gcdsingletest[2717:1303] executed 1 time(s)

2013-05-10 17:30:49.336 gcdsingletest[2717:c07] sharedinstance is ======><zymysingleton: 0x7195990>