iOS知识收集iOS 开发之 — 理论知识汇总iOS网络编程

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

2017-02-17  本文已影响4950人  CholMay

我们在开发中可能会遇到多个网络请求的需求,比如一个界面有两个请求,tableView的数据源及表头,需要等两个请求都完成才刷新UI,或者第二个的网络请求依赖第一个网络请求返回的数据,今天主要讲下两种方式的实现方式,当然也有其他方式,主要记录下我工作中使用的

并发执行

多个网络请求同时执行,等所有网络请求完成,再统一做其他操作,你可能会想到dispatch_group_asyncdispatch_group_notify结合使用,如下:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务一完成");
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务二完成");
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务三完成");
    });
    //在分组的所有任务完成后触发
    dispatch_group_notify(group, queue, ^{
        NSLog(@"所有任务完成");
    });

打印结果:

2017-02-17 09:26:18.668 信号量[53171:2916114] 任务二完成
2017-02-17 09:26:18.668 信号量[53171:2916133] 任务三完成
2017-02-17 09:26:18.668 信号量[53171:2916117] 任务一完成
2017-02-17 09:26:18.669 信号量[53171:2916117] 所有任务完成

由于三个任务都是异步的,所有返回结果顺序不一定是正序,看似却确实实现了我们的需求,但是网络请求是异步的,耗时的,不是马上就能返回结果的,如下使用dispatch_after模拟网络请求的延迟:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"请求一");
        });
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务二完成");
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务三完成");
    });
    //在分组的所有任务完成后触发
    dispatch_group_notify(group, queue, ^{
        NSLog(@"所有任务完成");
    });

打印结果:

2017-02-17 09:30:49.263 信号量[53528:2935734] 任务二完成
2017-02-17 09:30:49.263 信号量[53528:2935737] 任务三完成
2017-02-17 09:30:49.263 信号量[53528:2935737] 所有任务完成
2017-02-17 09:30:50.340 信号量[53528:2935638] 请求一

可看到任务一的延迟请求完成造成了一些问题,这时候我们可以用信号量即dispatch_semaphore,包括三个部分:dispatch_semaphore_create()dispatch_semaphore_signal()dispatch_semaphore_wait(),三个结合使用,各个参数解释一下:

  • dispatch_semaphore_createdispatch_semaphore_t dispatch_semaphore_create(long value);传入的参数为long类型,输出一个dispatch_semaphore_t类型且值为value的信号量。值得注意的是,这里的传入的参数value必须大于或等于0,否则dispatch_semaphore_create会返回NULL

代码示例:

   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_semaphore_signal(sem);
            NSLog(@"请求一");
        });
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    });
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_semaphore_signal(sem);
            NSLog(@"请求二");
        });
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"请求三");
        
    });
    //在分组的所有任务完成后触发
    dispatch_group_notify(group, queue, ^{
        
        NSLog(@"请求完成");
    });

打印结果:

2017-02-17 09:47:58.084 信号量[54431:3002639] 请求三
2017-02-17 09:47:58.958 信号量[54431:3002590] 请求二
2017-02-17 09:47:59.177 信号量[54431:3002590] 请求一
2017-02-17 09:47:59.177 信号量[54431:3002636] 请求完成

就实现了我们的需求(多个网络请求都完成再做其他操作),当然也可以通过dispatch_group_enter、dispatch_group_leave结合实现,使用dispatch_group_enter()函数进入到任务组中,然后异步执行队列中的任务,最后使用dispatch_group_leave()函数离开任务组即可,

dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"任务1");
            dispatch_group_leave(group);
        });
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务2");
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"任务3");
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"完成");
    });

打印结果:

2017-02-17 09:56:13.129 信号量[54888:3036359] 任务2
2017-02-17 09:56:13.129 信号量[54888:3036359] 任务3
2017-02-17 09:56:14.222 信号量[54888:3036264] 任务1
2017-02-17 09:56:14.222 信号量[54888:3036359] 完成

同样实现了我们需求

顺序执行

仍然通过信号量执行,执行任务一,将信号量dispatch_semaphore_wait减一,请求完成,再dispatch_semaphore_signal加一,一次类推,代码实现

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);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"1");
            dispatch_semaphore_signal(sem);
            
        });
    });
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        NSLog(@"2");
        dispatch_semaphore_signal(sem);
    });
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"3");
            dispatch_semaphore_signal(sem);
            
        });
    });
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        NSLog(@"4");
        dispatch_semaphore_signal(sem);
        
    });

打印结果:

2017-02-17 09:57:54.058 信号量[54991:3043275] 1
2017-02-17 09:57:54.059 信号量[54991:3043355] 2
2017-02-17 09:57:57.358 信号量[54991:3043275] 3
2017-02-17 09:57:57.358 信号量[54991:3043355] 4

这篇文章对信号量做了详细介绍
参考:
关于dispatch_semaphore的使用

上一篇下一篇

猜你喜欢

热点阅读