3

I am using NSURLSession in background mode, to upload images from file using a NSURLSessionUploadTask, and the delegate callbacks. All this works perfectly fine if the app remains on the background as opposed to getting killed by the user.

However if while there are still pending uploads, and the app is killed using the home screen, once the app is restarted and the images that did not upload get requeued as upload tasks with a new NSURLSession with the same background identifier, something really odd happens:

1) The tasks are created only once, and I assign a task description to them when that happens, also NSURLSession assigns them a task identifier which is unique per session. My NSURLSession background is a singleton.

2) The tasks fail almost immediately with this error:

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x174669880 {NSErrorFailingURLStringKey=myURL, NSURLErrorBackgroundTaskCancelledReasonKey=0

and it calls the completion delegate method:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

passing the aforementioned error above. At this point I print out the task identifier and the task description which matches the task I created.

3) After all this tasks fail almost immediately, that is when things start to get weird, all those tasks get somehow put again into the NSURLSession without my code doing anything, note that using Charles I can see a single request going out, which in reality never fails. This second time they succeed and they call the didCompleteWithError again this time there is no error. At this point printing the task description and task identifier, surprisingly returns the same task description I assigned, BUT a different task identifier!. Meaning iOS is somehow recreating that task with the same task identifier and queuing it up again, even though it claims it failed. Note that the network request can be seen in Charles only once and it never really fails

This is problem because I cannot distinguish between real upload errors, and this pseudo errors generated by iOS, right now since I am not explicitly canceling any of those requests, I am checking the error code for canceled, and choosing to ignore it, since I know the task will just get magically requeued. This seems like a pretty obvious bug to me, and it can be reproduced all the time.

With the workaround in place everything works as expected, but this is a hack, and I can imagine I might at some point ignore a real canceling and never notify the client that the request in fact failed.

I have only found a couple of people online describing the issue, but no responses.

Is there a radar filed for this?. Is there any way to prevent this from happening?

I am using the 8.1 SDK.

Oscar Gomez
  • 18,102
  • 12
  • 80
  • 113
  • Hi, I am trying to upload multiple images via NSURL Session in background but after 2-3 minutes process is stopped Can any one help me . Thanks in advance – Devil May 13 '15 at 12:37
  • This may rely to my problem: I can't cancel the `NSURLSessionUploadTask` operation the way obvious `NSURLSessionDataTask` do. It still uploads data to server. – Daniyar May 20 '15 at 14:30

0 Answers0