I'm coding resumable uploads using iOS 17's URLSession's
uploadTask(withResumeData:.
This function returns a non-Optional
URLSessionUploadTask
and does not throw.
In cases where the system determines the resumeData
is no longer valid, how do I detect that (so I can create a new URLSessionUploadTask
from scratch)?
I'm doing this for background uploads, so it's all URLSessionDelegate
apis, but what are the failure modes, and what Error
types and Codes
would we get specially?
Obviously, I expect the resume data is no longer usable or necessary when get a server success i.e. in the 2xx
range. Does the resume data also become invalid for other server responses, like 4xx's?
or 5xx's?.
I expect the resume data usually shouldn't become invalid when getting URLError's
like .networkConnectionLost,
since that's like half the point of having the feature in the first place, to resume after the a broken network connection. But I do expect that if the resumeData is invalid, then I should be able to reach the server and get a server response, so in that case what Code
would we get?
I'm assuming the system is caching our upload file somewhere, and the resume data somehow makes a reference to it, so does that file get optimized away at some point in time when left untouched, and need us to start a fresh upload? We are also saving the file for potential future re-uploads, until we get certain assurances of completion from our backend, but I am just wondering on which logic branches I need to determine that the resumeData
I thought I could use is no longer usable.
In cases where the system determines the resumeData is no longer valid, how do I detect that
You don’t need to. I think this quote is salient:
I expect the resume data usually shouldn't become invalid when getting [errors] like .networkConnectionLos
Actually, the resume data is invalid as soon as you use it. Once you start a task with resume data, one of two things will happen:
-
The system is able to resume, in which case it does so.
-
Or it’s not, in which case it’ll start from scratch.
Either way, the resume data is consumed. If the request then fails, that failure will come may or may not come with new resume data, and that’s what you use to retry.
Regarding the file, I recommend that you maintain the file on disk in its original location until the request is completed.
Finally, when testing this it’s good idea to do that testing in a standard session rather than a background session. Background sessions will typically auto resume, so it’s hard to exercise this flow there.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"