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.

Composites are, in many ways, like their own kind of settings container, just that they cannot contain<br />

additional containers. It’s important to not confuse the two. Child containers within settings are used<br />

only to create a hierarchy (refer to Scenario 3 in the sample). Composites, on the other hand, specifically<br />

exist to create more complex groups of settings that act like a single unit, a behavior that is not<br />

guaranteed for settings containers themselves.<br />

As noted earlier, these settings are all written to the settings.dat file in your app data Settings folder.<br />

It’s also good to know that changes you make to settings containers are automatically saved, though<br />

there is some built-in batching to prevent excessive disk activity when you change a number of values<br />

all in a row. In any case, you really don’t need to worry about when you save settings; the system will<br />

manage those details.<br />

Versioning App State<br />

From Windows’ point of view, local, temp, and roaming state are all parts of the same whole and all<br />

share the same version. That version number is set with<br />

Windows.Storage.ApplicationData.-setVersionAsync, the value of which you can retrieve through<br />

Windows.Storage.Application-Data.version (a read-only property). If you like, you can maintain your<br />

own versioning system within particular files or settings. I would recommend, however, that you avoid<br />

doing this with roaming data because it’s hard to predict how Windows will manage synchronizing<br />

slightly different structures. Even with local state, trying to play complex versioning games is, well, rather<br />

complex, and probably best avoided altogether.<br />

The version of your app data is also a different matter than the version of your app; in fact, there is<br />

really no inherent relationship between the two. While the app data version is set with<br />

setVersion-Async, the app version is managed through the Packaging section of the app manifest. You<br />

can have versions 1.0.0.0 through 4.3.9.3 of the app use version 1.0.0.0 of app data, or maybe version<br />

1.2.1.9 of the app shifts to version 1.0.1.0 of the app data, and version 2.1.1.3 moves to 1.2.0.0 of the app<br />

data. It doesn’t really matter, so long as you keep it all straight and can migrate old versions of the app<br />

data to new versions!<br />

Migration happens as part of the setVersionAsync call, whose second parameter is a function to<br />

handle the conversion. That function receives a SetVersionRequest object that contains<br />

currentVersion and desiredVersion properties, thereby instructing your function as to what kind of<br />

conversion is actually needed. In response, you should go through all your app data and migrate the<br />

individual settings and files accordingly. Once you return from the conversion handler, Windows will<br />

assume the migration is complete. Of course, because the process will often involve asynchronous file<br />

I/O operations, you can use a deferral mechanism like that we’ve seen with activation. Call the<br />

SetVersionRequest.getDeferral method to obtain the deferral object (a SetVersionDeferral), and call<br />

its complete method when all your async operations are done. Examples of this can be found in Scenario<br />

9 of the Application data sample.<br />

320

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

Saved successfully!

Ooh no, something went wrong!