Should files uploaded via uploadTask go in temp or caches directory?

When using uploadTask(with:fromFile:) to perform an upload in the background, should the fileUrl be kept in the temp directory:
Code Block
NSTemporaryDirectory()

Or should it instead be the Caches directory?
Code Block
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)

I ask because I am starting to see this crashing stack:

Code Block
Fatal Exception: NSInvalidArgumentException
Cannot read file at file:///private/var/mobile/Containers/Data/Application/XXXXXXXX-XXXX-XXXX-XXXX-CB39D5E080D9/tmp/org.alamofire.manager/multipart.form.data/XXXXXXXX-XXXX-XXXX-XXXX-28C54136FC0A


Coming from:
Code Block
Request.swift - Line 583partial apply for closure #2 in UploadRequest.Uploadable.task(session:adapter:queue:) + 583

And I suspect that the file is maybe being deleted while also being actively used by a background uploadTask?

The Apple File System basics docs say this about data in the temporary directory:

[you] cannot rely on these files persisting after your app terminates


It says this about caches:

Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file

NOTE: though this crashing stack is coming from Alamofire, I am in the process of re-writing it from scratch using pure-native Apple APIs, so assume for the purposes of this question that Alamofire is not part of the stack. Where should I be writing temporary files for upload?
Are you concern about NSURLSession background sessions? Or standard sessions? Or both?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Our concern is specifically about NSURLSession background sessions. We are starting (I'd say in the past 4 weeks) to have reports from users that various POST operations that happen via background sessions using uploadTask(with:fromFile:) are not completing successfully. We have an app-specific toggle that lets us disable the use of background sessions; when the users flip that toggle and now start using standard sessions only, their POST requests succeed.

FWIW, though we originally were using Alamofire, I rewrote our code to use NSURLSession directly last week, and we continued to have users who report upload issues. This suggests to me that we have a deeper issue/regression for some users and/or devices that use background sessions. Thus, I am wondering if, for example, a mis-use of the Caches vs the Temp directory on a device with storage constraints, for example, could explain this behavior.

Also note that we use background sessions for all POST operations because we want them to continue even if the user backgrounds the app. For example, the user sends a chat message then immediately backgrounds us; we want that to complete successfully, so we do all chat POST operations via background sessions. Perhaps using background NSURLSession while the app is foregrounded is itself mistaken?

Perhaps using background NSURLSession while the app is foregrounded is
itself mistaken?

No, that’s fine.

IIRC NSURLSession background sessions actually make a copy of the file (using APFS’s copy-on-write support, so it’s fast) when you kick off the upload, which runs counter to your theory.

This causes your app to crash, right? Can you post an Apple crash report for that? Use the text attachment feature (the paperclip icon) to avoid clogging up the timeline.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Should files uploaded via uploadTask go in temp or caches directory?
 
 
Q