Since I am uploading in the background, I need to save the request body off in a file. The documentation says this gets copied to a temporary storage area and uploaded from there. When can I delete the temporary file I generated? Deleting it just after the call to
session.uploadTask(with: request, fromFile: filePath)
seems to be a race condition where I will occasionally get a sharing violation deleting the file.
Do I have to keep my temporary file around until DidCompleteWithError or DidReceiveData is called?
I ask because I'm uploading an existing photo, so I have to generate a multi-part form file with the photo embedded, then iOS makes a copy of that file. This results in having the photo in storage on the device three times. We are uploading photos from an Event so there will be several hundred, so Im worried about device storage running out.
Todd
When uploading in the background, the temporary file should be deleted (on the main thread) in the upload task's completion handler.
That is, the upload task must have finished (with success or failure) before you delete the temporary file.
In URLSessionTaskDelegate, that would be in:
- urlSession(_:task:didCompleteWithError:)
(Of course, if there was an error, you might want to try again.)