NSURLSessionUploadTask is not calling delegate method with NSURLSessionConfiguration backgroundSessionConfiguration
Asked Answered
S

0

0

I am trying upload image using backgroundSessionConfiguration and NSURLSessionUploadTask to keep live upload process in application background mode.

But when i use backgroundSessionConfiguration, NSURLSessionUploadTask is not calling delegate method while using defaultSessionConfiguration it call delegate method.

Here is the code.

//Create a file to upload
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    NSData *imageData = UIImagePNGRepresentation(image);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
    NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];

NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.jpg"];
[imageData writeToFile:filePath atomically:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]];
[request setHTTPMethod:@"PUT"];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration: [NSString stringWithFormat:@"testSession.foo.com"]];
config.HTTPMaximumConnectionsPerHost = 1;
session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];


NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath]];
[uploadTask resume];
NSLog(@"==%ld",uploadTask.state);

if i change backgroundSessionConfiguration to defaultSessionConfiguration, delegate method will call.

Please help me to understand what am i missing.

Thanks, Nitin

Stearne answered 22/9, 2015 at 12:49 Comment(7)
You're looking at uploadTask.state immediately after starting task. In my experience, background sessions are a bit slower to start. Are you sure it's not firing off later? Did you implement URLSession:task:didCompleteWithError:? Is it called?Flub
@Flub : Yes i have implemented URLSession:task:didCompleteWithError but its not calling up even after 3-4 mins. I printed state of upload task just to confirm its state.Stearne
I might try backgroundSessionConfigurationWithIdentifier rather than the deprecated backgroundSessionConfiguration, but otherwise I don't see any problem. Make sure that's a valid object and that you're instantiating that only once in your code. Have you tried watching this in Charles to see if anything happens with your destination server.Flub
@Flub : yep now I am getting response. Thanks you very much. However, in background mode app stop printing logs after some time(2-3 mins) and if i take it foreground it start printing logs again.Stearne
First, I f you're seeing logs for 3 minutes, that makes me wonder if you did beginBackgriundTask... call somewhere. Generally you use background session or background task, but not both: you want to keep a minimal footprint after use leaves your app to go do something else.Flub
Second, if you're not seeing the app restart, I wonder if (a) you are responding to app delegate method handleEventsForBackgroundURLSession; (b) whether that's saving the completionHandler; (c) whether that is restarting the background session with same identifier; (d) whether your URLSessionDidFinishEventsForBackgroundURLSession is calling the saved completionHandler.Flub
Rob : Yep now getting server response, waited for 25 mins and put local notification since console log being stopped in background mode of app. thanks for your detail explanation it was very useful.Stearne

© 2022 - 2024 — McMap. All rights reserved.