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.

Basically, you would change the policy if you’ve prompted the user accordingly or allow them to set<br />

behavior through your settings. For example, if the user has a setting to disallow downloads or uploads<br />

on a metered network, your apps would set the general costPolicy to unrestrictedOnly. If you know<br />

you’re on a network where roaming charges would apply and the user has consented to a transfer,<br />

you’d want to change the costPolicy of that individual operation to always. Otherwise the API would<br />

not perform the transfer because doing so on a roaming network is disallowed by default.<br />

When a transfer is blocked by policy, the operation’s progress.status property will contain<br />

BackgroundTransferStatus.pausedCostedNetwork.<br />

Grouping Transfers<br />

The group property that’s found in BackgroundDownloader, BackgroundUploader, DownloadOperation, and<br />

UploadOperation is a simple string that tags a transfer as belonging to a particular group. The property<br />

can be set only through BackgroundDownloader and BackgroundUploader; you would set this prior to<br />

creating a series of individual operations. In those operations, the group property is available but<br />

read-only.<br />

The purpose of grouping is so that you can selectively enumerate and control related transfers, as<br />

we’ll see in the next section. For example, a photo app that organizes pictures into albums or album<br />

pages can present a UI through which the user can pause, resume, or cancel the transfer of an entire<br />

album, rather than working on the level of individual files. The group property makes the<br />

implementation of this kind of experience much easier, as the app doesn’t need to maintain its own<br />

grouping structures.<br />

The group has no bearing on the transfers themselves; it is not communicated to the server upload<br />

page.<br />

Suspend, Resume, and Restart with Background Transfers<br />

At the beginning of this section I mentioned that background transfers will continue while an app is<br />

suspended and paused if the app is terminated by the system. Because apps will be terminated only in<br />

low-memory conditions, it’s appropriate to also pause background transfers.<br />

When an app is resumed from the suspended state, it can check on the status of pending transfers by<br />

using the BackgroundDownloader.getCurrentDownloadsAsync and<br />

BackgroundUploader.getCurrent-UploadsAsync methods. In both cases two variants of the methods<br />

exist: one that enumerates all transfers, and one that enumerates those belonging to a specific group (as<br />

matches the group properties in the operations).<br />

The list that comes back from these methods is a vector of DownloadOperation and UploadOperation<br />

objects, and, as always, the vector can be addressed as an array. Code to iterate over the list looks like<br />

this:<br />

653

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

Saved successfully!

Ooh no, something went wrong!