Saving And Restoring State

I’ve just discovered a long-standing mistake in my code going back to 2013. Yikes!

Make sure to use system.ApplicationSupportDirectory and not system.CachesDirectory if you want state to persist between app launches.

From the docs on system.CachesDirectory:

Used with system.pathForFile() to create a path for storing and retrieving files that are available across application launches. This is ideal for saving state information.

I question that information in the docs.

I was using system.CachesDirectory and recently discovered that the contents of the directory are purged. In-app purchases, progress and other app state data can be lost if you use system.CachesDirectory to save state. I have now switched to system.ApplicationSupportDirectory

I hope you don’t make the same mistake as I did! More here:

Are you using system.ApplicationSupportDirectory to save state (e.g. progress and in-app purchases) in your app? I’d be interested to know your thoughts.

I’ve personally just used DocumentsDirectory for pretty much everything, and TemporaryDirectory for files that I don’t want to last for longer than a session.

1 Like

@XeduR that seems reasonable to me. From File System Basics on https://developer.apple.com/

DocumentsDirectory is for user data and generally includes any files you might want to expose to the user.

I’m assuming that system.DocumentsDirectory in Solar2D corresponds to iOS Documents/ folder.

system.ApplicationSupportDirectory includes files that the app uses to run but that should remain hidden from the user.

Either way, system.DocumentsDirectory and
system.ApplicationSupportDirectory
persist between app launches.

My Big Mistake was to use system.CachesDirectory - the docs claim that this directory is:

“Used with system.pathForFile() to create a path for storing and retrieving files that are available across application launches. This is ideal for saving state information.”

I will no longer use this directory for saving state information between app launches. I use it for downloading a config file from Dropbox each time an app launches.

You shouldn’t trust that any files saved to a user’s device are “hidden” from the user.

1 Like