0
votes

I am building an app that allow users to upload photos and send to my server. But then I notice that when I try to upload multiple photos, let say 5 photos, sometimes it only uploaded 2-4 images, and sometimes uploaded 5 images. The uploading process seems not so consistent, which I think has much to do with the network. Here are the codes for the uploading:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
dict = [returnString JSONValue];
[responseStr addObject:[[dict objectForKey:@"Image"]objectForKey:@"Name"]];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
ImageIndex = ImageIndex+1;
[self imageUplaodAndProductUploadFunction:ImageIndex];
}

Then I came up an idea to work around:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
dict = [returnString JSONValue];
if([responseStr containsObject:[[dict objectForKey:@"Image"]objectForKey:@"Name"]])
{
    NSLog(@"It present");
    itcontains =YES;
}
else
{
    itcontains = NO;
    NSLog(@"Not contain");
    [responseStr addObject:[[dict objectForKey:@"Image"]objectForKey:@"Name"]];
}

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(itcontains == NO)
{
   ImageIndex = ImageIndex+1;
}
[self imageUplaodAndProductUploadFunction:ImageIndex];
}

Now, it will upload 5 photos correctly, but then a weird situation happens: Let say I have 5 images, Image1, Image2, Image3, Image4 and Image 5. The 5 photos upload successfully, but the image might be duplicated: Case 1: Image1, Image2, Image2, Image4, Image4 Case 2: Image1, Image2, Image3, Image4, Image4

The duplicate can be any of the 5 images.

Can anyone help out? Thanks

2
I'm assuming (a) you're running these requests concurrently; and (b) you have the same instance acting as the delegate for all of these concurrent requests; and (c) you're referencing ivars where the various concurrent requests could conceivably step on top of each other. On top of that, you're using the deprecated NSURLConnection. If you use NSURLSession with completion block, a lot of these issues would solve themselves.Rob

2 Answers

0
votes

Thank you so much for the advice. Yes, I took a look into NSURLSession, and modify the code. Basically, I think the previous issues are now solve, I can now successfully upload 5 photos without problem.

0
votes

Please try NSURLSessionUploadTask

// Background upload only works with files

NSString *fileName = [NSString stringWithFormat:@"%@%@", IMAGE_NAME_TO_BE_SAVED, @".png"];
NSString *signatureFile = [NSString stringWithFormat:@"%@/%@",  NSTemporaryDirectory(), fileName];

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:signatureFile atomically:YES];

while (![[NSFileManager defaultManager] fileExistsAtPath:signatureFile]) {
    [NSThread sleepForTimeInterval:.5];
}

NSURLSession *session = [self backgroundSession];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uploadUrl]];
[request setHTTPMethod:@"POST"];
[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:signatureFile]];

[uploadTask resume];