Converting to Windows 8 from Windows Phone | Cache and working with storage (7 of 12)

Just as with most Windows Phone applications, you typically add value to a Windows 8 application when caching data. You gain some offline ability as well as a quick and snappy application that doesn’t need to go online and download data all the time.

In Reseguiden’s application we are not actually saving any data in our cache. This is because all data really need to be fresh so we really can’t cache it any longer than the application runs. But we do save previous searches (only the query, not the result) that makes it possibly for users to flip through previous searches in a pivot even in the application is restarted.

Saved searches

The plan is to implement the same functionality for the Windows 8 client.

This post is a part in a series – you can find the full index here.

The Windows Phone code

Today, when persisting to Windows Phone isolated storage we use the following code.

No magic, just simple code, but one thing to note – all is written synchronously. So if we don’t put this on a separate thread, than the UI thread, we risk freezing the UI while reading/writing data.

Different types of storage

Before we throw ourselves at the Windows 8 implementation, it is probably best to look a little bit closer at the different storage options that we have.

  • Local
    Works basically the same way as the storage that we are used to when developing for Windows Phone. Persists data locally on your device and the only way for a user to remove the data is to uninstall the application. You would typically put data here that you don’t need to share between devices and data that your application depends on to work.
  • RoamingData that syncs between devices (using the users live id). Here you typically place data containing settings and preferences. This storage should not be used to store large amount of data.
  • TemporarySimilar to local, the difference is that there are no guarantee that this data is there when your application asks for it. The user can clean it at any time using a disc cleanup. This basically means that the application never really can count on the data being there. Perfect to use as a cache.

So, in order to save our earlier searches we will use temporary storage. Thus allowing the user to clear old searches without us having to implement that functionality in our application.

Implementation for Windows 8

First of all, the one thing in common for all the storage types is that you access them all through Windows Runtime, compared with Windows Phone where we access storage through .NET. This means that our code will look quite different in comparison.

Since this is Windows 8 we got access to await and async which is good since any method call that potentially could take longer than 50ms is asynchronous in Windows Runtime.

Two thing worth mentioning here. First of all – we don’t need to use using since nothing needs to be disposed. Secondly – we do this completely asynchronously. So if we would have stored/read 5Mb JSON, this code would have been much better than the Windows Phone code.

Since we are using the temporary folder we also get the clear-cache-feature for free. They user doesn’t need to uninstall the application in order to clear whatever we are saving. We could, of course, have added functionality for clearing the cache in our application – but why bother if Windows 8 can do it for us?

2 Responses to “Converting to Windows 8 from Windows Phone | Cache and working with storage (7 of 12)”

  1. [...] Cache and working with storage [...]

  2. [...] around I discovered a setting that can enable debugging managed code in HTML applications.”Converting to Windows 8 from Windows Phone | Cache and Working with Storage (7 of 12) (Jayway Team Blog)“Just as with most Windows Phone applications, you typically add value to a [...]

Leave a Reply