watchOS temporary files

I am trying to understand the nature of the file system on watchOS (in particular the temporary directory), for the purpose of logging events in my long-running app. So far I have found out the following:

The temporaryDirectory URL looks like:

file:///private/var/mobile/Containers/Data/PluginKitPlugin/C46C4624-888D-4FBD-A125-894253D7ED2B/tmp

where the UUID embedded in the directory path changes for each build.

However, when I write to a log file ("log.txt")in that directory, I find that the file retains the data from build to build even though the UUID changes. So in one build, the log file URL directory is:

file:///private/var/mobile/Containers/Data/PluginKitPlugin/C46C4624-888D-4FBD-A125-894253D7ED2B/tmp/log.txt

and in the next build it is

file:///private/var/mobile/Containers/Data/PluginKitPlugin/FC472C31-9586-4880-9E72-3EECA372CAEF/tmp/log.txt

but the log data from the first build is retained in the second file!.

Coming only recently to watchOS from a long Unix and Windows background, I don't understand how a file in one directory can contain the same data as a file newly created in a different directory.

Replies

This Q has been sitting with statue "...but a moderator needs to approve it before it can be posted." for three days.

I wonder why?

I wonder why?

I’m not sure why. The most common trigger for moderation is URLs to non-Apple sites, but that’s not the case here.

Coming only recently to watchOS from a long Unix and Windows background, I don't understand how a file in one directory can contain the same data as a file newly created in a different directory.

This is related to the way that your app is installed. App installation on iOS-based platforms is not as simple as “copy the files to the system”. There’s a complex installation process and, as part of that process, your app’s container is given a new UUID.

The mechanics of this are complex, varying by platform (for example, watchOS apps aren’t really apps, put app extensions), OS version, and how your app was installed (Xcode vs standard user installs). You’ll also get a new container when the app is restored from a backup.

However the take-home message for developers is simple: do not save absolute paths. If you do, these paths will break when your container gets migrated. If you have to save paths, save them relative to the context in which you originally created them. For example, in this case it’s fine to save a path relative to the temporary directory.

Finally, none of this kicks in while your app is running, so it’s fine to work with absolute paths at runtime.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

OK, thanks for that, but I wonder if we could delve a little deeper?

I am trying to get a better handle on the file system on the Apple watch.

The documentationrefers to a "temporary directory" and promises a reveal with "For more information about temporary files, see File System Programming Guide." which specifically refers to the iOS file system and makes no reference to watchOS. Is there a similar document for watchOS?

I work on the assumption iOS & watchOS (both mobile platforms) are similar, if not identical. They would need to be by default, based on their direct design integration.


See:

https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1

Could you expand on "..watchOS apps aren’t really apps, put app extensions" please?

I am looking for the equivalent of an "app start" to initialize my state variables, but it seems that once the app has been started, it remains started even after the user has removed it from the Dock. I can't find a user operation that will clear the app from active memory, which would then trigger an app start event on a subsequent launch from the home screen.

Without spec's for the watchOS implementation there's no authorititive guide.

I agree that this can be confusing but you have to read a platform’s documentation from the perspective of its parent platform. watchOS is a ‘child’ of iOS and thus has much in common with iOS. The documentation tends to focus on the differences rather than the similarity.

We went through exactly the same process 10 years ago, when iOS split from macOS.

My immediate problem relates to the persistence of files in the temporary directory on watchOS. It seems to me that they persist over a watch reboot and an app upgrade. In which case, are they any different from permanent files?

watchOS has the same free space subsystem as iOS. While the mechanics of this are super complex and change from release to release, there are some standard expected behaviours:

  • You can absolutely rely on the system to not delete files out from underneath a running process. If your process is, say, suspended in the background, the system will terminate the process before deleting any files.

  • You can reasonable expect the system to not reclaim disk space unless it’s short on disk space.

Could you expand on "..watchOS apps aren’t really apps, put app extensions" please?

This is clearly described in App Programming Guide for watchOS, and specifically the The Watch App Architecture section.

I am looking for the equivalent of an "app start" to initialize my state variables …

What is your high-level goal here? Is it to test that initialisation code? Or something else?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"