23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Roaming State<br />

The automatic roaming of app state between a user’s devices is one of the most interesting and<br />

enabling features of Windows 8. There are few areas where a small piece of technology like this has so<br />

greatly reduced the burden on app developers!<br />

It works very simply. First, your app data roamingFolder and your roamingSettings container behave<br />

exactly like their local counterparts. So long as their combined size is less than Windows.Storage.-<br />

ApplicationData.current.roamingStorageQuota, Windows will copy that data to other devices where<br />

the same user is logged in has the same app installed; in fact, when an app is installed, Windows<br />

attempts to copy roaming data so that it’s there when the app is first launched.<br />

If the app is running simultaneously on multiple devices, the last writer of any particular file or setting<br />

always wins. When data has been roamed the other apps will receive the<br />

Windows.Storage.-ApplicationData.ondatachanged event. So your app will always read the<br />

appropriate roaming state on startup and refresh that state as needed within datachanged. You should<br />

always employ this strategy too in case Windows cannot bring down roaming state for a newly installed<br />

app right away (such as when the user installed the app and lost connectivity). As soon as the roaming<br />

state appears, you’ll receive the datachanged event. Scenario 5 of the Application data sample provides<br />

a basic demonstration of this.<br />

Deciding what your roaming experience really looks like is really a design question more than a<br />

development question. It’s a matter of taking all app settings that are not specific to the device<br />

hardware (such as settings that are related to screen size, video capabilities, or the presence of particular<br />

peripherals or sensors), and thinking through whether it makes sense for each setting to be roamed. A<br />

user’s favorites, for example, are appropriate to roam if they refer to data that isn’t local to the device.<br />

That is, favorite URIs or locations on a cloud storage service like SkyDrive, FaceBook, or Flickr are<br />

appropriate to roam; favorites and recently used files in a user’s local libraries are not. The viewing<br />

position within a cloud-based video, like a streaming movie, would be appropriate to roam, as would be<br />

the last reading position in a magazine or book. But again, if that content is local, then maybe not.<br />

Account configurations like email settings are often good candidates, so the user doesn’t have to<br />

configure the app again on other devices.<br />

At the same time, you might not be able to predict whether the user will really want to roam certain<br />

settings. In this case, the right choice is to give the user a choice! That is, include options in your Settings<br />

UI to allow the user to customize the roaming experience to their liking, especially as a user might have<br />

devices for both home and work where they want the same app to behave differently. For instance, with<br />

an RSS Reader the user might not want notifications on their work machine whenever new articles<br />

arrive, but would want real-time updates at home. The set of feeds itself, on the other hand, would<br />

probably always be roamed, but then again the user might want to keep separate lists.<br />

To minimize the size of your roaming state and stay below the quota, you might employ the<br />

Windows.Storage.Compression API for file-based data. For this same reason, never use roaming state for<br />

user data. Instead, use an online service like SkyDrive to store user data in the cloud, and then roam URIs<br />

to those files as part of the roaming experience. More details on using SkyDrive through its REST API can<br />

331

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!