Share via


Managing your App Data to build a continuous app experience

If you have an engaging app, you must be managing state and user generated data aka App Data. Depending on your requirements you could be using your own backend service to store this data but the Universal Windows Platform lets you manage that app data on a device, across Windows devices, across multiple users on a device and even across your suite of applications on a Windows device. If you are not already doing this, you should update your apps to take advantage of this feature set so that your users are never launching your app and finding themselves without their settings and data that they have generated which can be really frustrating and a major reason why they might never user your app again.

To illustrate how you can manage app data, I have a simple feed reader application source on GitHub. As the name suggests, you can configure this app with Feed urls and also specify the number of articles to download per url. The app downloads the feeds so that the user can read them offline. I will reference this app as I breakdown the various features that are available to manage app data.

Storage Locations

The application data management constructs are exposed in the Windows.Storage.ApplicationData class. Your app's data container has the following storage locations and depending on the location you pick, you get different behaviors. You must pick a location when you create settings or if you want to store files.

appdatacontainer

  1. Local - The setting and files that are placed in Local are specific to a device. Contents of local are backed up to OneDrive as part of application data backup and restore. During device set up users can choose to restore application data and this will restore all of the data that was backed up. In the SimpleReader app, we store the number of articles that should be downloaded as a local setting as this is device specific. Currently App Backup and Restore is only available on Windows Phone. Since this app backup consumes user OneDrive storage, your app should use this judiciously.
  2. Local Cache- So you want to store data that is only present on a device throughout the lifetime of an app, then you should be using Local Cache. In the SimpleReader application, we store the content of the articles in Local Cache. For example, in the SimpleReader application, instead of backing up the full article content (which goes stale quickly) in Local, we store this data in Local Cache.
  3. Roaming - As the name implies, settings and files that are created as Roaming are synced across Windows devices running against the same Microsoft Account. The contents of roaming are also downloaded when an app is being installed. Hence as a good practice, always check for the contents of Roaming before writing into it as you can end up overriding settings from another device. The Roaming data has a limit of 100K and is designed to sync your app data quickly. Your application must register for ApplicationData.DataChanged event so that it can react to changes that are being synced from another device.
  4. Temp - As the name suggests, this is the most volatile storage location. Data in this folder can be cleared by the system when required for example when there is low disk space. This is the location to put your intermediate files and the system will cleanup when required.

How do I create Settings ?

App Data is comprised of settings and files. Settings are the building blocks provided by the platform and these can be simple settings such as integer, Boolean, GUID etc. This is a key value pair. When you create a setting, you specify whether it is a LocalSetting or a RoamingSetting

 ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["#postsToDownload"] = 5;

or you can combine multiple simple settings into a Composite setting. For example in the SimpleReader app, I want to keep track of user defined urls with a friendly name. For this I created a Composite setting. This Composite setting is then roamed across devices as a RoamingSetting.

 ApplicationDataCompositeValue newFeedSubscription = new ApplicationDataCompositeValue();
newFeedSubscription.Add("FeedName", argFeedName);
newFeedSubscription.Add("FeedUri", argFeedURIString);

ApplicationDataContainer roamingSettings = ApplicationData.Current.RoamingSettings;
ApplicationDataContainer container = roamingSettings.CreateContainer("feedSubscriptions", ApplicationDataCreateDisposition.Always);
//If feed not already defined then add it to the container
if (!roamingSettings.Containers["feedSubscriptions"].Values.ContainsKey(myKey))
{
    roamingSettings.Containers["feedSubscriptions"].Values.Add(myKey.ToString(), myApplicationDataCompositeValue);
}

How do I save files?

When it comes to storing files, it is as simple as picking your StorageFolder and putting your files into in.

 StorageFolder myStorageFolder = await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
// Put files into the myStorageFolder

 

It is really that simple! Watch out for the next blog where I will walk you through how you can share data across multiple users of your application on a device and also between a suite of applications that you publish!

Let me know if you have any questions in the comment section!

Sandeep George, Program Manager - Windows Developer Platform.