iOS GCD之dispatch_group的使用(多个文件并发上传)

NSMutableArray *imageURLs= [NSMutableArray array];
dispatch_group_t group = dispatch_group_create();                    // 1
for (UIImage *image in images) {
    dispatch_group_enter(group);                                    // 2
    sendPhoto(image, success:^(NSString *url) {
        [imageURLs addObject:url];
        dispatch_group_leave(group);                                 // 3
    });
}
dispatch_group_notify(group, dispatch_get_global_queue(), ^{         // 4
    postFeed(imageURLs, text);
});

作者:liaojinxing
链接:http://www.jianshu.com/p/4769680b983d
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
首先创建一个dispatch_group
dispatch_group_enter往group中增加一个block任务,dispatch_group_leave则表示该block已经执行完成。需要注意的是,enter和leave必须配对。在这个例子当中,每一个block任务就是上传一张图片。

Calling this function increments the current count of outstanding tasks in the group. Using this function (withdispatch_group_leave
) allows your application to properly manage the task reference count if it explicitly adds and removes tasks from the group by a means other than using the dispatch_group_async
function. A call to this function must be balanced with a call to dispatch_group_leave
. You can use this function to associate a block with more than one group at the same time.
当group中之前所有提交的block任务都执行完后,dispatch_group_notify中的block任务就会被执行。在此例中,所有的图片都上传成功之后,就会发布动态。

作者:liaojinxing
链接:http://www.jianshu.com/p/4769680b983d
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值