0

I am trying to upload more then 200 images (as per my need) When application in background. I am using NSUrlSession (upload task). It is uploading approx. 20 images easily but after that process is not responding. I am using a single request (Not using an array for upload images on server for client requirement). Please suggest me any idea with some example code, Because i have tried with approx. 10-15 different approach.

Thanks is advance

Devil
  • 314
  • 3
  • 13
  • 1
    Use background threading – vijeesh May 13 '15 at 13:11
  • possible duplicate of [iOS: Perform upload task while app is in background](http://stackoverflow.com/questions/23829546/ios-perform-upload-task-while-app-is-in-background) – pteofil May 13 '15 at 13:16

2 Answers2

1

Application goes to suspended state (In Background) after a specific time . So all the ongoing tasks remain suspended.

Possible approach is , Application can request OS for some extra time to complete the unfinished task using beginBackgroundTaskWithExpirationHandler.

A sample code is below:

__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);

[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
backgroundTaskIdentifier = backgroundTaskIdentifier;
}];

NOTE: For 200 images as you are mentioning, it will be a tough task for the application to be in active state. Try different approach, for example giving support to App for Background modes.

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
  • Thanks for answer "Abdul" as you saying i am trying background mode approach but when app gone in suspended mode all background process are stop.. – Devil May 14 '15 at 04:43
0

Try this

I am using the AFNetworking for doing this

Step 1: create your request

NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                       URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD]
                                                                                      parameters:param constructingBodyWithBlock:^(id formData) {

       [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] 
                                  name:@"sendimage" 
                              fileName:[strImagePath lastPathComponent] 
                              mimeType:@"image/png"
                                 error:nil];
                                   } error:nil];

Step 2: here I am creating the Stream at given Location

[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 
                                      writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] 
                                                completionHandler:^(NSError *error){

Step 3: here I am creating uploading task.

    ///here is file
    NSProgress *progress = nil;
    NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1
                                                                    fromFile:[NSURL fileURLWithPath:strImagePath]
                                                                    progress:&progress
                                                           completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                                   NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error);

                                                           }];
    [uploadTask resume];
}
                                                }];

}

repest this code for uploading with background task

Sumeet Mourya
  • 434
  • 1
  • 7
  • 23
  • @NirmitDagly - strImagePath : This is the path of the image which I stored in Document Directory. – Sumeet Mourya Dec 10 '15 at 06:54
  • I've written image data in the files in Document Directory as you per your sample code. But now what problem I'm facing is that When I use AFNetworking to upload image on server, the name of the image is being saved but file size remains zero. Please help me to solve this issue because my requirement is same as yours and I've to upload 100 images on server. – Nirmit Dagly Dec 10 '15 at 07:43