If I begin the transfers in the background (call the [uploadtask resume] while the app is in the background), and the discretionary property is automatically set to YES, what exactly does this mean?
To start, understand that the “discretionary property” isn’t an actual property, at least not one that you can see. Rather, it’s a value computer by NSURLSession based on a number of criteria, including:
The type of session (standard vs background)
property of the configuration used to create that session
Whether the app was in the foreground when the task was created
Whether the app is in the foreground right now
Moreover, the specific effects of this ‘property’ are not guaranteed; they have changed in the past and I fully expect them to change in the future.
Does the phone have to be on wifi AND plugged in for it transfer or just be on wifi?
In general, yes. However, it’s really up to the OS as to when it schedules discretionary tasks, and it’s possible that, for example, if the device ‘learns’ that it’s never on Wi-Fi, it may chose to run discretionary tasks over WWAN.
What if I bring the app back to the foreground, is the discretionary property for these transfers set back to NO (given that session.discretionary = NO when I setup the background session)?
Again, no property values change here but, yes, modern versions of iOS include the foreground state of the originating app when determining whether to run a discretionary request.
I setup and initiate around a 100 upload tasks to a NSURLSession object. If I lose network connectivity and regain it a while later (say 1 hour), does the session automatically resume all pending upload tasks?
Again, the exact behaviour is not specified and depends on a bunch of things. But, yes, I would expect these tasks to eventually complete.
(the URLSession didSendBodyData is no longer being called, so I'm guessing all of the tasks got cancelled somehow)
You shouldn’t need to guess here. If a task gets cancelled you should be told about it (via the standard task completion mechanism). You can also confirm whether a task is still around using
I expect what you’ll find is that the remaining tasks are still around, they’re just not running right now for some reason (lack of Wi-Fi, lack of power, resource budgets, and so on).
Share and Enjoy
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"