利用GCD实现 5秒请求一次接口

基础知识:

同步、异步的主要影响:能不能开启新的线程
并发、串行的主要影响: 任务的执行方式

GCD 使用的2个步骤:(最有价值的用法:将异步任务添加到并发队列中)

定制任务:确定想做的事情
将任务添加到队列中:GCD会自动将队列中的任务取出,放到对应的线程中执行。

要执行一个任务,你需要将它添加到一个适当的dispatch queue,你可以单个或按组来添加,也可以同步或异步地执行一个任务。一旦进入到queue,queue会负责尽快地执行你的任务。
一般可以用一个block来封装任务内容。


    #define HLGlobaQueue  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

%new
- (void)run{
    %log();
    // while (1) {
        // @synchronized(self) {//加锁


    dispatch_group_t group = dispatch_group_create();


    dispatch_group_async(group, HLGlobaQueue, ^{


            while (1) {

                [NSThread sleepForTimeInterval:5];



        
            NSMutableDictionary *param = [[NSMutableDictionary alloc] init];

        NSString  *url = [TaokeHttpTool setupUrlWithUrlMethod:@"getSearchKey"];//处理URL
        [TaokeHttpTool postWithURL:url params:param success:^(NSDictionary *response) {

                     //处理是否延时睡眠

                    // NSTimeInterval timeout =  [data[@"Timeout"] integerValue];
                    // NSLog(@"Timeout:%f",timeout);
                    // [NSThread sleepForTimeInterval:timeout];
}RspCDFailed:nil failure:nil];



}

    });






// }

        // }//解锁
    // }
}

%end

你可能感兴趣的:(利用GCD实现 5秒请求一次接口)