美文网首页
iOS 顺序执行

iOS 顺序执行

作者: _Waiting_ | 来源:发表于2018-05-08 14:27 被阅读20次
//创建一个全局队列
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
        //创建一个信号量(值为0)
       dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        dispatch_async(queue, ^{
             NSLog(@"执行 === 1");
            [GroupRecordAPI getDadaWith:1 andCondition:nil success:^(id obj) {
                 NSLog(@"执行 === 2-1");
           dispatch_semaphore_signal(semaphore);
            } failure:^(id obj) {
                NSLog(@"执行 === 2-2");
                dispatch_semaphore_signal(semaphore);
            }];
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"执行 === 3");
        });

打印结果

2018-05-08 14:25:45.734599+0800 uAcademic[392:104334] 执行 === 1
2018-05-08 14:25:46.809465+0800 uAcademic[392:104283] 执行 === 2-1
2018-05-08 14:25:46.809808+0800 uAcademic[392:104334] 执行 === 3

GCD 信号量控制并发 (dispatch_semaphore)

相关文章

网友评论

      本文标题:iOS 顺序执行

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