利用GCD,使异步转同步

dispatch group

手动操作group

enter则引用数+1
leave则引用数-1
当引用数为0的时候,wait会继续等待
当引用数为1的时候,wait不会等待

@strongify(self, upload);
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
// 异步上传附件
[self uploadAtt:upload finish:^(BOOL isSuccess, id responseObject) {
    if (isSuccess) {
       [upload deleteInRealm];
    }
    dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
可以将wait替换为: 
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // 待引用数为0之后,异步唤醒
});

自动操作group, dispatch_group_async

* Submits a block to a dispatch queue and associates the block with the given
* dispatch group
//将一个block(代码块)加入到dispatch_queue_t queue中并和dispatch_group_t group相关联
void dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block);

将代码块dispatch_block_t block放入队列dispatch_queue_t queue中执行;并和调度组dispatch_group_t group相互关联;如果提交到dispatch_queue_t queue中的block全都执行完毕会调用dispatch_group_notify, 并且dispatch_group_wait会停止等待;

dispatch semaphore

我是没咋用过,信号量计数方法比较繁琐
详见

你可能感兴趣的:(利用GCD,使异步转同步)