Talha Ch Talha Ch - 26 days ago 8
Objective-C Question

Run a method when AFMultipartFormData is finished uploading

I'm sending multiple images to server using

AFMultipartFormData
using a for loop to fetch one image at a time:

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSError *error;
NSString *mystring = @"noName";
//FOR Loop Start
for(NSData *eachImage in dataStringArray) {

NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFormData:eachImage name:@"myImage"];
[formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
name:@"FileName"];
} error:&error];

NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
return;
}

[uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];

}];
[task resume];
}//for end
[self makeJSON:uploadedImageIDs];


But somehow it calls
makeJSON
method before. As there are multiple images so I need it to call after the uploading is finished for all images.

Answer

Network processing will run by asynchronous so you need to count in network completionHanlder for knowing when all task finished. Like this

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    NSError *error;
    NSString *mystring = @"noName";
    NSInteger count = 0;

//FOR Loop Start
    for(NSData *eachImage in dataStringArray) {

        NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

            [formData appendPartWithFormData:eachImage name:@"myImage"];
            [formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding]
                                        name:@"FileName"];
        } error:&error];

        NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error) {
                NSLog(@"%@", error.localizedDescription);
                return;
            }
            count++
            [uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
            if (count == dataStringArray.count) {
              [self makeJSON:uploadedImageIDs];

            }
        }];
        [task resume];
    }//for end
Comments