I was recently working on an issue about missing files when needed or when users export them. When I looked at the implementation, I did not understand what was wrong at first sight. I was considering that there was a silent failure or some other reason beyond the directory, because the problem emerged sporadically. Anyways, after reading how it is created and fetched from the system, I found out the actual problem: the directory.

I’ve used them before, a lot of times, but never came across an issue about the misusage/wrong directory selection. Therefore, I needed to read this documentation again and carefully learn the different behaviors of these directories.

The rule is simple: if the document that you create should not be deleted at all unless you or the user wants to delete it, you should use documentsDirectory or applicationSupportDirectory. If the user should not access this file, the latter is the choice. If they can access it, then the former. If the stored data is necessary for only one session or it does not matter how long it lives, you should use cachesDirectory or temporaryDirectory. The former might live longer than the latter, but no promise. You can assume the latter as a one-time usage file. Imagine it goes away when the app is terminated or suspended. My experience showed that the cachesDirectory is not cleared for a long time, but there is no clear separation at which point this one is deleted or distinguished from temporaryDirectory. Therefore, in my opinion, the comparison loses its meaning, but the rule is that you should be fine with loss of these data. If it does not matter how long it lives, but it is useful as long as it lives and is not necessary to keep forever, then use cachesDirectory.

Source: Apple Developer Documentation: Using the File System Effectively