What happens to the
NSURLSessionDownloadTask
when the expiring activity expires before the request finishes?
Note I’m going to phrase this in terms of UIApplication background tasks, because that’s what most folks use.
-performExpiringActivityWithReason:usingBlock:
is directly analogous to that API.
Also, if you haven’t read my UIApplication Background Task Notes post, you should do that now.
When your background task time expiries your process gets suspended. The exact behaviour of network connections in this case depends on:
How long you’re suspended
Whether socket resource reclaim runs
When the system gets around to terminating your app
Note For more information about socket resource reclaim, see Technote 2277 Networking and Multitasking for details on that.
There are four common scenarios:
You’re resumed promptly, in which case the request will complete successfully.
You’re not resumed for a while, in which case the request will fail with a timeout.
Socket resource reclaim runs, in which case the request will fail with an unspecified error.
The system terminates your app, in which case you lose all track of the request
In my opinion it’s better to avoid getting into this situation entirely. If you’re using a high-level API like
NSURLSession
, I generally recommend that you divide your requests up into categories:
For short-running interactive requests, cancel the request when your background time expires.
For large, long-running requests, use an
NSURLSession
background session, which will allow the request to continue while your app is suspended (or terminating).
There are alternative schools of thought here. For example, for a short-running interactive request you could do nothing and then deal with any error you get. As good error handling is a critical part of any networking app, you have to write that error handling code anyway.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"