API Tip: Don’t Abuse File.applicationStorageDirectory

It was recently pointed out to me that some applications, which shall remain nameless here, are using File.applicationStorageDirectory to cache hundreds of megabytes of files. Please, don’t do this.

The File.applicationStorageDirectory property is intended to provide applications with a guaranteed writable, unique location in which they can save important information, regardless of which platform they’re running on. The information is saved to locations that are, on typical machines, backed up. On Windows, it’s saved under “<user>/Application Data”, which also roams with the user, if roaming profiles are enabled. There’s an implicit assumption here that the amount of data being stored is relatively small–megabytes at most.

Large sets of cached files should be stored elsewhere, in locations that aren’t backed up and don’t roam. AIR doesn’t include a handy property for finding such a location, but don’t let that stop you; the filesystem API lets you access any location on disk, after all. On Windows, you can use the “<user>\Local Settings\Application Data” directory (note the “Local Settings”); on Mac OS, use “<user>/Library/Caches”.

8 Responses to API Tip: Don’t Abuse File.applicationStorageDirectory

  1. Quentin says:

    That’s interesting.So that means we would need another (native) cross-platform static variable…And that “applicationStorageDirectory” name might not be the best.Damn.

  2. Ryan says:

    I would be wary of using ~/Library/Caches on OS X to store large amounts of data.First off, there are many, many instructions out there that tell users to periodically delete all the contents of this directory to optimize their machines, as you’ll see if you Google “os x library/caches”. So if you are storing “hundreds of megabytes” of data here, and it gets dumped, I imagine it taking your app quite some time to rebuild all that, if it the original source of that data is even still available (that much stuff probably came either from the Internet, which means hours to days of re-downloading, or from some removable storage like a CD/DVD, which is probably not in the drive anymore and maybe even lost & forgotten by the user because they thought they were done with it after “installing” all this stuff).On the flip side, if a user thinks are they are removing your app by throwing it in the Trash from out of the Applications folder, as is common on a Mac, and they aren’t a performance-tuning, cache-clearing junkie, they’re likely going to be removing your 10MB application and leaving behind those hundreds of megs of data, just wasting space on their drive!I would argue that if dumping this big chunk of data on your drive is presented to the user in any way as part of some kind of “install”, you should consider putting it with the application, in /Applications. Yeah, I know there might be permissions issues to deal with. But users are used to authenticating and stuff when they think of their task as “installing.”Your other good bet is /Library/ApplicationSupport or ~/Library/ApplicationSupport, based on this document: http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPFileSystem/Articles/WhereToPutFiles.htmlFor “back up everything” solutions like Apple’s Time Machine, this stuff is going to get backed up — as it should, if it’s really “back up everything,” the purpose of which is usually to be able to restore your machine in one blast and have everything pretty much work like it did before. Other backup solutions are more user-configured, so it’s going to be up to them to decide what they want anyway. In both cases, you can give the user some info to help them make an informed decision if they’re interested.

  3. Aaron says:

    I agree with Quentin. The name “applicationStorageDirectory” says to me that I can store files. Something like “applicationPreferencesDirectory” would be better.+1 for adding a cross platform place to store “non roaming” files.Is this documented anywhere? If not that would help too.

  4. Ian says:

    Oops. The problem is I don’t know jack about the Mac, so the value that AIR provides is abstracting that away from me.Another system var in 2.0 would be appreciated.On the up side, I delete the cache every time the application closes.

  5. Daniel says:

    Where is the best directory for storing helper applications (called with NativeProcess) that are going to modify and save files?As described elsewhere, this can’t be the install directory (due to Windows Vista and 7 restrictions).As described above, looks like ApplicationStorageDirectory isn’t the place.What’s the best place to put an .exe such that the workingDirectory can be set to that path and the file will run without restrictions?My helper apps are orphans.[Helper applications are best run from the application install directory because (a) it’s the most secure location for an executable and (b) you know your application has permission to execute programs found there. If the helper app modifies files when it runs, then just the files should be placed elsewhere. Where those files should go depends on what kind of files they are. —Oliver]

  6. Aaron Hardy says:

    Thanks for this post. It’s very helpful if not a bit disgruntling. I too think it would be great to have an abstracting variable referring to a location to store longer-term, large, application-specific files. The current documentation makes no mention of what’s been written in this article. While long-time desktop devs will probably understand the consequences of using applicationStorageDirectory, most AIR developers are probably coming from the web and have no idea. Although I understand what you’re saying about only storing small amounts of data here, the documentation stating “This directory is a convenient location to store application-specific data.” doesn’t clarify the issue or tip off the developer that they should research more. It would be nice if it did, FWIW.

  7. OMA says:

    What about iOS? If it’s not a good idea to use applicationStorageDirectory, what’s the alternative in that OS? I need to store in the iPad a few not too big videos downloaded from my app (30 MB or so). Is the appStorageDirectory not a good option? (even if I mark those files as “do not backup” with the native iOS extension for that purpose)