iOS知识库

iOS信号量处理异步请求中的同步处理

2017-12-07  本文已影响578人  程序员不务正业
 // 创建信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    // 创建全局并行
    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, ^{

        // 请求一
        [loginCode getUserInfoWithNick:nil andUserId:kUserId onSuc:^(id data) {
            NSLog(@"yue");
            dispatch_semaphore_signal(semaphore);

        } andFail:^(NSError *error) {       
        }];

    });
    dispatch_group_async(group, queue, ^{

        // 请求二
        [CommodityViewModel getPriceTransformForIntegral:nil onSuccess:^(id data) {

            NSLog(@"duihuan11");
            dispatch_semaphore_signal(semaphore);

        } onFailure:^(NSError *error) {
        }];
    });
    dispatch_group_async(group, queue, ^{

        // 请求三
        [CommodityViewModel getPriceTransformForIntegral:nil onSuccess:^(id data) {
            NSLog(@"duihuan22");
            dispatch_semaphore_signal(semaphore);

        } onFailure:^(NSError *error) {            
        }];
    });

    dispatch_group_notify(group, queue, ^{

        // 三个请求对应三次信号等待
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        //在这里 进行请求后的方法,回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{

            //更新UI操作

        });
    });

2018/05/27上方dispatch_semaphore和dispatch_group其实是重复了的。使用dispatch_semaphore或者dispatch_group其中一个就够了

dispatch_group使用

dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //请求1
        NSLog(@"第一个请求");
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //请求2
        NSLog(@"第二个请求");
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //请求3
        NSLog(@"第三个请求");
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //界面刷新
        NSLog(@"回到主线程刷新页面");
    });

dispatch_semaphore使用

// 设定信号值=2 一次执行两个等待,必须得等两个等待结束后再次进入第三个等待
 dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 任务一
    dispatch_async(quene, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"111");
        sleep(1);
        NSLog(@"complete task 1");
        dispatch_semaphore_signal(semaphore);
    });
    
    // 任务二
    dispatch_async(quene, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"222");
        sleep(1);
        NSLog(@"complete task 2");
        dispatch_semaphore_signal(semaphore);
    });
    
    // 任务三
    dispatch_async(quene, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"3333");
        sleep(1);
        NSLog(@"complete task 3");
        dispatch_semaphore_signal(semaphore);
    });

dispatch_barrier_async使用

 dispatch_queue_t queue = dispatch_queue_create("threadgcd", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"111");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"222");
    });
    dispatch_async(queue, ^{
        NSLog(@"333");
    });
    
    dispatch_barrier_async(queue, ^{
        for (int i = 0; i < 5000000; i++) {
            if (50000 == i) {
                NSLog(@"point");
                sleep(5);
            }
        }
    });
    
    dispatch_async(queue, ^{
        NSLog(@"444");
    });
上一篇 下一篇

猜你喜欢

热点阅读