2

I am developing a app which upload multiple files. For uploading, I use AFHTTPRequestOperation. It successfully works, But If I lock and after it unlock the screen,then It stop uploading files.

My code for upload file is here

NSUserDefaults *defaultUser = [NSUserDefaults standardUserDefaults];
NSString *userId = [defaultUser stringForKey:@"UserId"];

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",UploadURL,userId]]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:nil];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:nil parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFileData: data name:@"f" fileName:[NSString stringWithFormat:@"%d_image.jpeg",rand()] mimeType:@"image/jpeg"];
    }];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", [operation error]);
        if(error.code == -1001){
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                          message:@"The request timed out." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [myAlert show];
        }
    }];


[operation start];

Can anyone give me suggestion for handle this situation.

Thanks.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Play cool
  • 158
  • 4
  • 12
  • It's something to do with setShouldExecuteAsBackgroundTaskWithExpirationHandler but I'm not 100% sure where that code has to go – Nick Jan 21 '13 at 18:37
  • http://stackoverflow.com/a/9164755/1702413 – TonyMkenu Jan 22 '13 at 23:09
  • @TonyMkenu that's for NSURL connection, not AFNetworking? – Nick Mar 05 '13 at 13:47
  • @Nick yes (just un example), but .. As of AFNetworking 1.0RC1, AFURLConnectionOperation has a setShouldExecuteAsBackgroundTaskWithExpirationHandler: method that can be used to have operations continue when an app is dismissed and enters the background https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ – TonyMkenu Mar 05 '13 at 14:09
  • @Nick http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues – TonyMkenu Mar 05 '13 at 14:13
  • @TonyMkenu thanks for the link, I'm using it in a faux singleton (as suggested on the github page), and just can't work out how to apply that method – Nick Mar 07 '13 at 13:04

1 Answers1

2

(1) Add this code:

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
    // Handle iOS shutting you down (possibly make a note of where you
    // stopped so you can resume later)
}];

(2) Change this line:

[operation start];

to

[client enqueueHTTPRequestOperation:operation];

(3) Keep a strong reference to your AFHTTPClient (this is typically done for AFHTTPClient with the singleton pattern) so that your operations don't get deallocated

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287