See the description first
What is the requirement: multiple network requests to upload images in the for loop, one is uploaded at a time. As for why only one is uploaded at a time, because uploading one will return to the address of the image storage server, and then use the address as a request parameter to request the API of your own server.
The process can be said to be tortuous, but there are also gains
Solution 1:
Recursively call the function to upload the image, after the first request is successful, then the second one is carried out in turn. Of course, the efficiency is definitely not high, even if the AFN method of uploading pictures is multi-threaded. The upload failed in the middle. Will you continue to upload? Demand decision. All Give Up
Solution 2:
Use GCD queue group: Here is the code directly:
dispatch_group_t group = dispatch_group_create(); for(..){ dispatch_group_async(group,dispatch_get_global_queue(0, 0), ^{ NSLog(@"Quote Group: There is a time-consuming operation to complete!"); }); } dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"Quote Group: All the previous time-consuming operations have been completed, go back to the main thread for related operations"); });
I can only say that the idea is perfect, and I don’t just propose this kind of solution online, but there are problems with my practice process. Network requests are delayed and queue groups are uncontrollable. So give up
Solution 3: Use the order of arrays:
How to use it? Please see:
Create a model, two attributes, the first data information you want to upload, such as: image or data, based on the parameters you need to upload the function. The second attribute: NSString URL The image address returned by the server:
The array of images you want to upload is encapsulated into a model array. The array is ordered. This is the key point. For loop The model calls the image upload function as a parameter. After the request is successful, the value of the model is updated.
Determine that all pictures have been uploaded, then, take them from the model array, and ask for your own server API
OK, it should be very clear. If you don't know, please add some more information:
model:
// The purpose of establishing this class is mainly to upload multiple images for loop and return after success#import @interface FMUploadModel : NSObject //Do decide according to your upload function needs@property (nonatomic,strong) NSArray * dataArray; @property (nonatomic,copy) NSString *imgUrl; @end function //Open a multi-thread to perform image upload work dispatch_queue_t queue = dispatch_queue_create("upimgs", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ // Time-consuming operation is placed here __weak typeof(self) weakself = self; __block int finishNum = 0; for (int i = 0 ; i < ; i++) { YRFormData *getData = [[YRFormData alloc] init]; = UIImagePNGRepresentation([i]); = @"uzee_image"; = @""; = @"image/png"; NSArray *array = @[getData]; //***********// FMUploadModel *model = [FMUploadModel new]; = array; = @""; [models addObject:model]; /********* is actually passed one by one. The array is transmitted to call this method************/ [YRHttpTool postWithURLNoAES:@"*****" params:@{} formDataArray: success:^(id json) { finishNum ++ ; = json[@"response"][@"cloud"]; if (finishNum == ) { dispatch_async(dispatch_get_main_queue(), ^{ // Go back to the main thread for UI operations [MBProgressHUD hideHUDForView:]; updateImage(); }); } } failure:^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ // Go back to the main thread for UI operations [MBProgressHUD hideHUDForView:]; [MBProgressHUD showError:@"Image upload failed, please try again later" toView:]; }); } progress:^(NSProgress *progress) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ float value = 1.0 * / ; [MBProgressHUD showDownload:@"Releasing..." toView: progress:value]; }]; }]; } });
Summarize
The above is the iOS problem and solution for uploading multiple images and address return order that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!