How to use data caching during network operations (XAML)
[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]
This topic shows how to use data caching with network operations in a Windows Runtime app.
Caching network content as app data
Storing content to disk lets your app behave fast and fluidly in situations where an app is unexpectedly terminated or use of a network is limited. For example, an RSS feed reader app can immediately display feeds that were cached to disk from a previous session. Once the latest feeds are available, the app can update its content. This ensures that the user has content to sift through immediately upon launch while the new content is updating.
Windows 8 provides the ApplicationData class in the Windows.Storage namespace. This class provides access to the app data store. Application data is mutable data that is specific to a particular app. It includes runtime state, user preferences, and other settings. Application data is created, read, updated, and deleted when the app is running. For more information, see Accessing app data with the Windows Runtime.
Files transferred by your app through network operations can be cached by existing as app data in either the Roaming, Local, or Temporary folders:
Folder | Description |
---|---|
Local | Files remain on the machine on which they were originally written and are not synchronized with other devices. |
Temporary | Files are subject to deletion when not in use. The system considers factors such as available disk capacity and the age of a file when determining when or whether to delete a temporary file. |
Roaming | Files are synchronized across devices where users have signed in with connected accounts. Roaming of files is not instant; the system weighs several factors when determining when to send the data. Usage of roaming data should be kept below the quota (available via the RoamingStorageQuota property), or else roaming of data will be suspended. Files cannot be roamed while an app is still writing to them, so be sure to close your app’s file objects when they are no longer needed. |
The code snippets below demonstrate caching content, in the form of a .txt file from a network operation, as app data to the Roaming folder. The code then demonstrates how to retrieve any content cached within the folder at a later time.
Examples
First we define a reference to Roaming folder. Next, our cacheResponse example creates a new file within the Roaming folder, and indicates that any existing file with the same name should be replaced. Once the file has been created, the contents are written to the new serverResponse.txt file from the file originally returned with the server response.
StorageFolder roamingFolder = null;
const string filename = "serverResponse.txt";
async void cacheResponse(string strResponse)
{
StorageFile file = await roamingFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, strResponse);
}
To access the cached serverResponse.txt file at a later time, our getCachedResponse example retrieves the file by name, defined by filename, and displays the text it contains.
async void getCachedResponse()
{
try
{
StorageFile file = await roamingFolder.GetFileAsync(filename);
string response = await FileIO.ReadTextAsync(file);
}
catch (Exception)
{
// getFileAsync or readTextAsync failed
// No cached response
}
}
Caching content included with the server response as app data will allow for fast access and display following an app termination and relaunch. For more details on writing settings to the app data store and how to respond to roaming events, read Managing app data or download the Application data sample.
Related topics
Quickstart: Local application data
Quickstart: Roaming application data