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

IOS开发(63)之GCD执行延迟操作

程序员文章站 2022-06-07 15:01:06
1 前言 使用dispatch_after ,能够在你想指定一定数量的延迟之后,使用 gcd 来执行代码。今天我们就来学习一下。 2 代码实例 demo1: zyapp...

1 前言

使用dispatch_after ,能够在你想指定一定数量的延迟之后,使用 gcd 来执行代码。今天我们就来学习一下。


2 代码实例
demo1:

zyappdelegate.m


[plain]
- (void) printstring:(nsstring *)paramstring{ 
    nslog(@"%@", paramstring); 

 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 

    /* 
     推迟三秒执行printstring方法 
     withobject:传的参数 
     */ 
    [self performselector:@selector(printstring:) withobject:@"grand central dispatch" afterdelay:3.0]; 
    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; 

- (void) printstring:(nsstring *)paramstring{
    nslog(@"%@", paramstring);
}

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
    /*
     推迟三秒执行printstring方法
     withobject:传的参数
     */
    [self performselector:@selector(printstring:) withobject:@"grand central dispatch" afterdelay:3.0];
    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;
}
运行3秒后控制台结果:


2013-05-10 17:04:52.710 gcdaftertest[2385:c07] grand central dispatch

demo2:

zyappdelegate.m

 

[plain]
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 

    //设置时间为2 
    double delayinseconds = 2.0; 
    //创建一个调度时间,相对于默认时钟或修改现有的调度时间。 
    dispatch_time_t delayinnanoseconds =dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec); 
    //推迟两纳秒执行 
    dispatch_queue_t concurrentqueue =dispatch_get_global_queue(dispatch_queue_priority_default, 0); 
    dispatch_after(delayinnanoseconds, concurrentqueue, ^(void){ 
        nslog(@"grand center dispatch!"); 
    }); 
     
    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; 

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
    //设置时间为2
    double delayinseconds = 2.0;
    //创建一个调度时间,相对于默认时钟或修改现有的调度时间。
    dispatch_time_t delayinnanoseconds =dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec);
    //推迟两纳秒执行
    dispatch_queue_t concurrentqueue =dispatch_get_global_queue(dispatch_queue_priority_default, 0);
    dispatch_after(delayinnanoseconds, concurrentqueue, ^(void){
        nslog(@"grand center dispatch!");
    });
   
    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;
}
运行2秒后控制台结果

 

2013-05-10 17:06:27.023 gcdaftertest2[2435:1303] grand center dispatch!

demo3:

zyappdelegate.m

 

[plain]
void processsomething(void *paramcontext){ 
    nslog(@"processing..."); 

 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 

    //设置时间 
    double delayinseconds = 2.0; 
    dispatch_time_t delayinnanoseconds = 
    dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec); 
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); 
    //调用c函数processsomething 
    dispatch_after_f(delayinnanoseconds, concurrentqueue, 
                     null, 
                     processsomething); 
    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; 

void processsomething(void *paramcontext){
    nslog(@"processing...");
}

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
    //设置时间
    double delayinseconds = 2.0;
    dispatch_time_t delayinnanoseconds =
    dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec);
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
    //调用c函数processsomething
    dispatch_after_f(delayinnanoseconds, concurrentqueue,
                     null,
                     processsomething);
    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;
}
运行2秒后控制台结果

 

2013-05-10 17:07:27.660 gcdaftertest3[2476:1303] processing...