iOS开发 GCD一些常用的方法

来源:互联网 发布:linux c va list 编辑:程序博客网 时间:2024/05/26 15:54

1.创建主线程(串行)

dispatch_async(dispatch_get_main_queue(), ^{        //刷新界面代码    });

2.创建异步线程(并行)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        //比较耗时的代码放这里    });

3.创建异步线程(并行)

double delayInSeconds = 1.0;    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        //延迟代码    });

4.单例

static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        //只执行一次代码    });

5.有三个任务,需要异步并发执行前两个任务,前两个任务执行完成后再执行第三个任务

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{                //创建组        dispatch_group_t group=dispatch_group_create();                // 关联一个任务到group        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{            //任务一            NSLog(@"******执行任务一******");        });                // 关联一个任务到group        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{            //任务二            NSLog(@"******执行任务二******");        });                // 等待组中的任务执行完毕,回到主线程执行block回调        dispatch_group_notify(group, dispatch_get_main_queue(), ^{            //任务三            NSLog(@"******等待组中的任务执行完毕,回到主线程执行block回调,执行任务三******");        });            });

6.dispatch_barrier_async的使用,dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行

dispatch_queue_t queue = dispatch_queue_create("create_asy_queue", DISPATCH_QUEUE_CONCURRENT);    dispatch_async(queue, ^{        NSLog(@"dispatch_async1");    });    dispatch_async(queue, ^{        NSLog(@"dispatch_async2");    });    dispatch_barrier_async(queue, ^{        NSLog(@"dispatch_barrier_async");        dispatch_async(dispatch_get_main_queue(), ^{            NSLog(@"刷新界面");        });            });    dispatch_async(queue, ^{        [NSThread sleepForTimeInterval:1];        NSLog(@"dispatch_async3");    });

7.GCD的另一个用处是可以让程序在后台较长久的运行.在没有使用GCD时,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是在使用GCD后,app最多有10分钟的时间在后台长久运行.这个时间可以用来做清理本地缓存,发送统计数据等工作

// AppDelegate.h文件    @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;        // AppDelegate.m文件    - (void)applicationDidEnterBackground:(UIApplication *)application    {        [self beingBackgroundUpdateTask];        // 在这里加上你需要长久运行的代码        [self endBackgroundUpdateTask];    }        - (void)beingBackgroundUpdateTask    {        self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{            [self endBackgroundUpdateTask];        }];    }        - (void)endBackgroundUpdateTask    {        [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];        self.backgroundUpdateTask = UIBackgroundTaskInvalid;    }

8.重复执行

__block NSInteger timeout = 5; //倒计时时间dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC, 0); //每秒执行dispatch_source_set_event_handler(_timer, ^{    if (timeout <= 0) { //倒计时结束,关闭        dispatch_source_cancel(_timer);        dispatch_async(dispatch_get_main_queue(), ^{            // 在这里执行事件        });    } else {        timeout--;    }});dispatch_resume(_timer);

原创粉丝点击