1

I like to use the NSURLSession in backgroundSessionConfiguration with the method:

uploadTaskWithRequest:fromFile:

But I need to run some code after the upload to select the next file. When selected, I like to start the NSURLSession again to upload it with the uploadTaskWithRequest:fromFile: method

So uploading a bunch of files recursively

Do you know if that is possible?

Bart Schoon
  • 299
  • 5
  • 11
  • Have you implemented the delegate method? The app delegate methods? `application:handleEventsForBackgroundURLSession:completionHandler:` sounds like what you need. – Léo Natan Dec 31 '13 at 09:13

1 Answers1

1

Short answer is yes, its possible. You have 30 seconds to do this.

  1. What ever code you run, you have to make sure that it'll run if the app is rebooted in the background. i.e deal with everything in memory being dropped out and reinitializing them. This is because while your app is in the background, if the user is running other memory heavy apps, your app will get killed due to memory pressure. However a kill from memory pressure will still result in your application waking up at some point after the NSURLSession task is completed.

  2. I would not recommend it. Based on my experience, chaining upload requests one at a time like this is extremely ineffective because you are waiting for the app to be woken up once per every single upload you do. Waking an app up is expensive and does not happen that often or reliably at all. It depends on the usage behavior of the user not the state of your upload.

My recommendation is to configure your NSURLSession configuration to be serial (if you want it to process one task at a time per host, I dont think you can actually serialize tasks if they are connecting to different hosts.) and then submitting all your upload tasks at once and let nsnetworkd take care of scheduling them and submitting them. In my experience this was orders of magnitude more reliable and much much faster.

Hope that helps.

maxf
  • 411
  • 4
  • 5
  • Can you configure NSURLSession to be serial? I wasn't aware of such a setting? – Glen T Apr 23 '14 at 22:29
  • 2
    Per host you can. There's a setting for NSURLSessionConfiguration called HTTPMaximumConnectionsPerHost. If you set this to be 1, this guarantees a single connection at a time but only to that host hence my above answer. Hope that helps. – maxf Apr 24 '14 at 04:52