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
NSURLConnection
. If you useNSURLSession
with completion block, a lot of these issues would solve themselves. – Rob