iOS 利用GCD实现任务并发处理

2020-04-11  本文已影响0人  eden_lx

在日常开发中会遇到等待任务完成,再去处理其他操作,比如多请求等等,目前实现的是模拟的多图上传功能,具体上代码

NSMutableArray *imgurlArr = [NSMutableArray array];

[MWTools showHUDProgress];

dispatch_group_t group = dispatch_group_create();

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

dispatch_group_async(group, queue, ^{

for (int i = 0; i < self.imageArr.count; ++i) {

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

UIImage *img = self.imageArr[i];

[MWUpLoadFileRequest requestUpLoadImageWithImage:img Completion:^(NSString * _Nonnull imageUrl) {

[imgurlArr addObject:imageUrl];

dispatch_semaphore_signal(semaphore);

}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

}

dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[MWTools dismissHUD];

NSString *urls = [imgurlArr componentsJoinedByString:@";"];

[MWFeedbackSettingRequest requestFeedbackSettingWithBody:@{@"text":cell_a.inputTextView.text,@"urls":urls} Completion:^(BOOL isSuccessful) {

    if (isSuccessful) {

    [MWTools showWithTitle:@"提交成功"];

    [self.navigationController popViewControllerAnimated:YES];

    }

}];

});

});

上一篇 下一篇

猜你喜欢

热点阅读