IOS 学习笔记--单个页面多次请求-排队处理

业务需求:一个页面加载2次请求,保证两次请求完成后,在页面加载数据。

参考:主要参考方法

_group = dispatch_group_create();
_semaphore = dispatch_semaphore_create(0); 
dispatch_queue_t queue=dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);

   for(intj=0; j<2; j++) {

       dispatch_group_async(_group, queue, ^{

           if(j==0){

                 [_KYNetgetmanuGateways:@(1):@(1000):[NSNumbernumberWithLong:coor.latitude]:[NSNumbernumberWithLong:coor.longitude]];

           }else{

               NSDictionary *param = @{@"pageIdx":@(1), @"limit":@(1000),@"sensorType":@(-1),@"status":@(-1),@"areaIDJSONArray":@"[\"aaa\"]"};

                  [_KYNetgetSuperSNList:param :nil];

           }

//            dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);

       });

   }

   dispatch_group_notify(_group, queue, ^{

       dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);

       dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);

       //所有请求返回数据后执行

       dispatch_async(dispatch_get_main_queue(), ^{

            [self refresh];

       });

   });

注意: 是为了在主线程进行数据加载

dispatch_async(dispatch_get_main_queue(), ^{

             [self refresh];

        });

推荐阅读更多精彩内容