Is it possible to achieve request deduplication using two URLSession
instances?
Just so we’re clear, the referenced WWDC presentation says:
URLSession
will deduplicate any in-process requests under the hood.
I’ve no idea what the speaker was trying to convey there. URLSession
can cache responses but AFAIK it will not deduplicate inflight requests.
So we should instead launch a data task and have the delegate "catch"
the cancel error and retry it as a download task?
The exact mechanics of how you do this are up to you. In a Swift concurrency world you could implement it by catching the error. Alternatively, you could just cancel one task and then start the download immediately. That simplifies the startup at the cost of making the commit more complex.
You mentioned "cancel that and retry it as a download task". Are we
responsible for canceling the tasks when the app transitions to the
background, or can we rely on the system doing that?
The tasks in a standard session will fail if your app gets suspended. Exactly how they fail is a complex question. IMO you’d be better off taking control of this yourself by explicitly cancelling them.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"