美文网首页
网络异步请求同步、顺序处理

网络异步请求同步、顺序处理

作者: 小如99 | 来源:发表于2018-09-03 17:54 被阅读34次
    一、异步网络请求按顺序请求(信号量)
    dispatch_semaphore_t sem = dispatch_semaphore_create(1);
        dispatch_queue_t queue = dispatch_queue_create("testBlock", NULL);
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            [self.requestObj fetchHomeBannerDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----hahahahahah");
                dispatch_semaphore_signal(sem);
            }];
        });
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            [self.requestObj fetchGetAdvertisementCompletion:^(BOOL success) {
                NSLog(@"lililili ----babababababa");
                dispatch_semaphore_signal(sem);
            }];
        });
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            [self.requestObj fetchGetPropagandaInfo:^(BOOL success) {
                NSLog(@"lililili ----cacacacacaca");
                dispatch_semaphore_signal(sem);
            }];
           
        });
        dispatch_async(queue, ^{
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            [self.requestObj fetchHomeRigsterDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----dadadadadada");
                dispatch_semaphore_signal(sem);
            }];
            
        });
    

    打印结果

    2018-09-03 17:36:57.714339+0800 xx[6989:1205370] lililili ----hahahahahah
    2018-09-03 17:36:57.783999+0800 xx[6989:1205370] lililili ----babababababa
    2018-09-03 17:36:57.912055+0800 xx[6989:1205370] lililili ----cacacacacaca
    2018-09-03 17:36:58.027282+0800 xx[6989:1205370] lililili ----dadadadadada
    
    二、多个网络异步请求最后同步(Group)
      dispatch_group_t dispatchGroup = dispatch_group_create();
        dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    
        dispatch_group_enter(dispatchGroup);
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchHomeBannerDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----hahahahahah");
                dispatch_group_leave(dispatchGroup);
            }];
        });
        
        dispatch_group_enter(dispatchGroup);
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchGetAdvertisementCompletion:^(BOOL success) {
                NSLog(@"lililili ----babababababa");
                dispatch_group_leave(dispatchGroup);
            }];
        });
        
        dispatch_group_enter(dispatchGroup);
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchGetPropagandaInfo:^(BOOL success) {
                NSLog(@"lililili ----cacacacacaca");
                dispatch_group_leave(dispatchGroup);
            }];
        });
        
        dispatch_group_enter(dispatchGroup);
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchHomeRigsterDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----dadadadadada");
                dispatch_group_leave(dispatchGroup);
            }];
        });
    
        dispatch_group_notify(dispatchGroup, queue, ^{
    
            NSLog(@"请求完成");
        });
    

    打印结果

    2018-09-03 17:32:20.433136+0800 xx[6912:1183702] lililili ----babababababa
    2018-09-03 17:32:20.439339+0800 xx[6912:1183702] lililili ----cacacacacaca
    2018-09-03 17:32:20.440473+0800 xx[6912:1183702] lililili ----hahahahahah
    2018-09-03 17:32:20.442021+0800 xx[6912:1183702] lililili ----dadadadadada
    2018-09-03 17:32:20.442153+0800 xx[6912:1184110] 请求完成
    
    三、多个网络异步请求最后同步(Group+信号量)
      dispatch_group_t dispatchGroup = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_semaphore_t dsema = dispatch_semaphore_create(0);
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchHomeBannerDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----hahahahahah");
                dispatch_semaphore_signal(dsema);
            }];
            dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
        });
    
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchGetAdvertisementCompletion:^(BOOL success) {
                NSLog(@"lililili ----babababababa");
                dispatch_semaphore_signal(dsema);
            }];
            dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
        });
    
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchGetPropagandaInfo:^(BOOL success) {
                NSLog(@"lililili ----cacacacacaca");
                dispatch_semaphore_signal(dsema);
            }];
            dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
        });
    
        dispatch_group_async(dispatchGroup, queue, ^{
            [self.requestObj fetchHomeRigsterDataCompletion:^(BOOL success) {
                NSLog(@"lililili ----dadadadadada");
                dispatch_semaphore_signal(dsema);
            }];
            dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
        });
    
        dispatch_group_notify(dispatchGroup, queue, ^{
    
            NSLog(@"请求完成");
        });
    

    打印结果

    2018-09-03 17:51:16.829153+0800 xx[7329:1270405] lililili ----babababababa
    2018-09-03 17:51:16.833271+0800 xx[7329:1270405] lililili ----dadadadadada
    2018-09-03 17:51:16.835973+0800 xx[7329:1270405] lililili ----hahahahahah
    2018-09-03 17:51:16.903278+0800 xx[7329:1270405] lililili ----cacacacacaca
    2018-09-03 17:51:16.903525+0800 xx[7329:1270499] 请求完成
    

    参考文章:

    多个网络请求并发执行、顺序执行

    相关文章

      网友评论

          本文标题:网络异步请求同步、顺序处理

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