第三方sdk有个异步上传方法 [UploadManager upload],但是只能单文件上传,要批量上传,得多次调用这个方法。现在我想得到他们全部结束的通知。
我尝试了用gcd group的方法,
dispatch_group_t group = dispatch_group_create();
for (NSData *data in array) {
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
[UploadManager upload];
});
}
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// 汇总结果
});
但是因为[UploadManager upload]是异步的,汇总结果总是一开始就先执行了。有什么办法解决吗?
dispatch_group_t serviceGroup = dispatch_group_create();
// Start the first service
dispatch_group_enter(serviceGroup);
[self.configService startWithCompletion:^(ConfigResponse *results, NSError* error){
// Do something with the results
configError = error;
dispatch_group_leave(serviceGroup);
}];
// Start the second service
dispatch_group_enter(serviceGroup);
[self.preferenceService startWithCompletion:^(PreferenceResponse *results, NSError* error){
// Do something with the results
preferenceError = error;
dispatch_group_leave(serviceGroup);
}];
dispatch_group_notify(serviceGroup,dispatch_get_main_queue(),^{
// Assess any errors
NSError *overallError = nil;
if (configError || preferenceError)
{
// Either make a new error or assign one of them to the overall error
overallError = configError ?: preferenceError;
}
// Now call the final completion block
completion(overallError);
});
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。