zoukankan      html  css  js  c++  java
  • IOS开发(63)之GCD执行延迟操作

    1 前言

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

    2 代码实例

    Demo1:

    ZYAppDelegate.m

    - (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

    - (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

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

    3 结语

    以上是所有内容,希望对大家有所帮助。

    Demo代码下载:http://download.csdn.net/detail/u010013695/5353603

  • 相关阅读:
    lvs持久连接及防火墙标记实现多端口绑定服务
    LVS负载均衡器DR模型的实现
    CentOS 6.5结合busybox完成自制Linux系统及远程登录和nginx安装测试
    CentOS 6.5下的lamp环境rsyslog+MySQL+loganalyzer实现日志集中分析管理
    centos中selinux功能及常用服务配置
    网站遭遇CC及DDOS攻击紧急处理方案
    centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网
    centos 6.5内核编译步骤及配置详解
    centos系统初始化流程及实现系统裁剪
    iptables实现网络防火墙及地址转换
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3072152.html
Copyright © 2011-2022 走看看