Windows Phone 8.1 for Developers – Application Data

This blog post is part of a series about how Windows Phone 8.1 affects developers. The series is written in collaboration with Microsoft evangelist Peter Bryntesson, check out his blog here.

Introduction

In this blog post I will do a quick overview of what is new with application data in Windows Phone 8.1. This is the first impression of the functions so I do recommend you to take a look at MSDN if you want a deeper understanding of how it works.

What is the same as before

Saving data to local storage works as before. Here is a code example of how it could look like:

What is new

Even before you could instead of targeting LocalFolder target RoamingFolder or TemporaryFolder. This code did compile however it did throw a not implemented exception when you ran the code. In Windows Phone 8.1 it is now implemented. The code structure is the same as before, just change what folder you’re targeting. This is exactly as it works on Windows 8.1.

Another new function which is very useful is the FileIO.WriteTextAsync. This makes it very easy to write text to files:

TemporaryFolder

Why use the TemporaryFolder? This is the place where you can save data without thinking of have to remove it later. The data is saved between sessions but is removed when Windows Phone deem it necessary, e g running low on memory. Saving data between sessions is perhaps not the best idea but TemporaryFolder can be used for example for sharing data between several view when you for some reason don´t want to keep the data in memory. You can save images to the temporary folder for use in your views if you don´t want to persist them in your local folder. From XAML you can target the TemporaryFolder by using ms-appdata:///temp/. Example of image XAML:

RoamingFolder

This is a very useful function. If you save data to the RoamingFolder the data is available on every device the app is installed, of course only if you log in with the same id. Perhaps it is more useful on Windows Store apps because often you have more than one device. But now when the stores will merge one can roam data between a Windows 8.1 app and a Windows Phone 8.1 app, just set the same app id to both apps in the store.

If you want to find out when roaming data has been changed by someone else you need to listen to DataChanged. This fires if roaming data is change by another app:

RoamingStorageQuota

How much data can you roam? By calling RoamingStorageQuota you get the amount of data possible to roam. If you try to roam more data than RoamingStorageQuota specifies the system stop replication the data until it below the limit again. The normal amount of data possible to roam is 100kb.

Settings

The settings saving is also the same as Windows 8.1. Instead of targeting LocalStorage it is possible to target LocalSettings, there is also a RoamingSettings. This works the same as folders does, local saves locally and roaming saves to the cloud. How do settings work then? Quite easy actually, just fetch the settings and save:

In addition to save text to a setting one can save a composite value. This is a setting which contains several settings:

It is also possible to create containers in the settings. This for making it easier to structure the settings. Example of a container:

If you use RoamingSettings changes can also be detected using the

Version

If you´re saving data and update your app you might need to use versioned data. Versioning enables you to change the application data format used in a future release of your app without causing compatibility problems with previous releases of your app. The app checks the version of the data in the data store, and if it is less than the version the app expects, the app should update the application data to the new format and update the version.

The version start at zero and can be obtain by checking

if the version is lower than the expected to some converting and set the version to the correct one

Summary

It is very nice to have the same data handling as in Windows 8.1. It is now possible to roam, save settings etc. This makes it easier to develop and share code between Windows Phone 8.1 and Windows 8.1 projects.

This Post Has 6 Comments

  1. One of the most long-awaited features on Windows Phone is definitely VPN support and now finally you can protect your entire mobile browsing, eliminate online restrictions and stay 100% private on Wi-Fi hotspots with premium-class VPN service.
    http://www.supervpn.net/

  2. In the second block of coud you should write “await tempFolder” :)

    1. Correct, a typo.

  3. Thanks for the article Robert. Can you please tell me what happens to files in TemporaryFolder when the app is suspended?

    Thanks

    1. TemporaryFolder is emptied when the phone needs the memory. It can be there after a suspention but then again it may not.

Leave a Reply

Close Menu