3

Surprisingly, and after some time debugging this, I've found that my app is purging files from tmp directory after 1 minute or so, without any other user action, even while the app is running on the foreground.

Accordingly to the docs, temporary directory should keep the files during app execution and only remove it afterwards or at most on its startup.

I don't want to move/copy to caches directory as workaround here, I'm more surprised about why is this actually happening. If that helps, I'm picking from Files app (using UIDocumentPickerViewController).

To make it even better, sometimes there are files that persist for a long time there, even though they are older than the others that are removed instantly after picking. This can result in bad access because your user selects a file, go grab a coffee while the app is still running, and then presses the button to upload it somewhere or whatever and the file is gone.

I can replicate it everytime with a simple app.

Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87
  • Just checking the obvious - you are using the correct tmp dir and not deleting those files yourself e.g. when some variable goes out of scope? Maybe post some code ... an easy test would be to use a 'normal' dir if you can easily change the dir in your code. – skaak Jul 07 '20 at 04:36
  • No, basically I’m just using the `UIDocumentPickerController` and then on the didPick I’m returning it’s path. (This is a pod code to be used as a plugin). It takes around a minute or so before the files disappear. If you print the files in `tmp` folder after picking, it’s there. Then, you wait 1 minute, print again and there are not there. That’s the odd part. – Miguel Ruivo Jul 07 '20 at 10:05
  • Strange things happen when you do that! When you pick a doc it typically gets duplicated and the path changes weirdly (weirdly relative to what a normal person might expect, but I digress). When you pick the document it gets duplicated and often the ```UIDocument```'s ```writeContentsToUrl ... originalUrl...``` gets messaged. That creates a duplicate and then remove the original so files can change in a weird way. Apple does this to allow you to easily rewind but basically everything gets duplicated while you save. I suspect this is why the new Apple filesystem is so good at duplicating files. – skaak Jul 07 '20 at 11:21
  • Note - I *suspect* this is behind your trouble. That path can change during the duplication and the document may open the duplicate somewhere else, so it is still there, only not in the original place you expect. – skaak Jul 07 '20 at 11:30
  • What is more strange, is that it shouldn't remove the files while the app is still running, but it actually does. By reading [at the docs](https://developer.apple.com/documentation/foundation/1409211-nstemporarydirectory) it says that retrieves the path for current user. This is quite misleading as it suggests that this tmp directory is shared by all apps, when in fact, each app seems to have its own. – Miguel Ruivo Jul 07 '20 at 13:37
  • Strange - can you show some code? – skaak Jul 07 '20 at 19:12
  • Did you make any progress on this? Experiencing the same issue. The docs only say the temporary URLs cannot be relied upon after the app terminates which imply they shouldn't be deleted while the app is running. Super weird. – tombardey Feb 06 '21 at 09:18
  • Unfortunately not. I probably ended up using other directory or so, I really don't remember. – Miguel Ruivo Feb 06 '21 at 16:14
  • Was there enough space on disk? Could it be that they were remove because of space issue? Is a strange behavior, I admit. – Larme Feb 07 '22 at 21:49
  • @Larme in my case, the user I believe this is happening to has about 16GB of free space. – Mick MacCallum Feb 08 '22 at 14:35
  • An apple bug maybe. The same issue is in [https://stackoverflow.com/a/48007752/11662833](https://stackoverflow.com/a/48007752/11662833). Seems like moving the file to another location is the only solution. – Sreekuttan Feb 09 '22 at 08:52
  • Are you actually keeping the file open? Temporary files that are not being kept open will be purged. – Adam Waldenberg Feb 11 '22 at 13:04

1 Answers1

1

After working with temp files a bit, I don't believe this is a bug, it is a feature for temporary files to be deleted when not opened for a period of time. I do believe you can manage that more directly by differentiating between normal and temporary files. As per a much more detailed article here, you can use the ManagedURL protocol to fix your issue:

public protocol ManagedURL {
    var contentURL: URL { get }
    func keepAlive()
}

public extension ManagedURL {
    public func keepAlive() { }
}

extension URL: ManagedURL {
    public var contentURL: URL { return self }
}

To keep the background operation alive:

URLSession.shared.uploadTask(with: request, fromFile: fileToUpload.contentURL) { _, _, _ in
    temporaryFile.keepAlive()
}
EJZ
  • 1,142
  • 1
  • 7
  • 26