使用GCD

作者: 何大双 | 来源:发表于2018-06-21 15:59 被阅读0次

代码块只执行一次

(1)创建单例

+ (instancetype)cacher {

    static MHPCacher*cacher =nil;

    static dispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        cacher = [MHPCachernew];

    });

    returncacher;

}

(2)防止重复执行代码块,如点击事件

static dispatch_once_t disOnce;

dispatch_once(&disOnce,^ {

    if([_delegate respondsToSelector:@selector(rewardDetailHeaderBtnTouchDelegateSelector:)]) {            

    [_delegate rewardDetailHeaderBtnTouchDelegateSelector:1001];

    }

});

延时

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    CHPChildController vc =self.childViewControllers[self.selectedButton.tag-1];

    vc.loadPage =1;

    [vc requestListData];

});

网络请求队列

            dispatch_group_tgroup =dispatch_group_create();

            dispatch_queue_t q = dispatch_get_global_queue(0, 0);

            dispatch_group_enter(group);

            for(NSMutableDictionary*parameDicinpostArray) {

                dispatch_group_async(group, q, ^{

                    [AFHttp requestWihtMethod:MyRequestMethodTypePost url:@"api/Objects/newPraise" params:parameDic success:^(id response) {

                        NSLog(@"执行完成%@", parameDic);

                        //删除对应的数据

                        NSString*oidStr = [NSStringstringWithFormat:@"%@",parameDic[@"oId"]];

                        [[MHPCachercacher]clearCacheWith:PraiseTypeAndid:oidStr];

                    }failure:^(NSError*err) {

                    }];

                });

            }

            dispatch_group_notify(group, dispatch_get_main_queue(), ^{

                NSLog(@"所有队列完毕 %@", [NSThreadcurrentThread]);

                dispatch_group_leave(group);

            });

异步加载图片回到主线程刷新UI

__weak __typeof (self)weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       NSURL *url = [NSURL URLWithString:adModel.sharePic];        

        weakSelf.shareImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

        dispatch_async(dispatch_get_main_queue(), ^{

        self.shareImageView.image = weakSelf.shareImage;

});

});

创建定时器

-(void)gcdTimer{

    //定义队列

//声明

@property (nonatomic, strong) dispatch_source_t timer;

//创建

-(void)createGCDTimer{

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    //创建定时器

    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);

    dispatch_time_tstart =DISPATCH_TIME_NOW;        //当前时间

    dispatch_time_tinterval =1.0*NSEC_PER_SEC;    //间隔时间  --秒

    //设置定时器

    dispatch_source_set_timer(self.timer, start, interval,0);

    //设置回调

    dispatch_source_set_event_handler(self.timer, ^{

        NSLog(@"-------%@",[NSThread currentThread]);

        [selfcountDownTimerSelector];

    });

}

//使用

            dispatch_resume(self.timer);


相关文章

  • iOS-多线程:GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(6种不同组合区别) GCD 线程间的通信...

  • iOS多线程--彻底学会多线程之『GCD』

    GCD 文章目录 GCD简介 任务和队列 GCD的使用步骤 队列的创建方法 任务的创建方法 GCD的基本使用 并行...

  • iOS GCD

    GCD 简介 GCD 任务和队列 GCD 的使用步骤 GCD 的基本使用(六种组合不同区别,队列嵌套情况区别,相互...

  • 多线程之GCD

    GCD介绍 1、GCD简介 2、GCD任务和队列 3、GCD 的基本使用 4、GCD 线程间的通信 5、GCD 的...

  • iOS 关于GCD很详细的描述

    那为什么我们要使用 GCD 呢? 因为使用 GCD 有很多好处啊,具体如下:GCD 可用于多核的并行运算;GCD ...

  • GCD多线程详解

    1. GCD 简介 2. GCD 任务和队列 3. GCD 的使用步骤 4. GCD 的基本使用(6种不同组合区别...

  • iOS多线程--GCD篇

    GCD 文章目录GCD简介任务和队列GCD的使用步骤队列的创建方法任务的创建方法GCD的基本使用并行队列 + 同步...

  • iOS实录16:GCD使用小结(二)

    iOS实录16:GCD使用小结(二) iOS实录16:GCD使用小结(二)

  • iOS GCD的使用

    什么是GCD了解GCD前,需要了解的基础知识GCD的使用使用注意事项 -GCD学习前铺垫-什么是GCDGCD (G...

  • iOS - GCD

    目录 GCD简介 GCD核心概念 GCD队列的使用 GCD的常见面试题 GCD简介 Grand Central D...

网友评论

      本文标题:使用GCD

      本文链接:https://www.haomeiwen.com/subject/mrxcyftx.html