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.

Windows.Networking.BackgroundTransfer.BackgroundDownloader.getCurrentDownloadsAsync()<br />

.done(function (downloads) {<br />

for (var i = 0; i < downloads.size; i++) {<br />

var download = downloads[i];<br />

}<br />

});<br />

Windows.Networking.BackgroundTransfer.BackgroundUploader.getCurrentUploadsAsync()<br />

.done(function (uploads) {<br />

for (var i = 0; i < uploads.size; i++) {<br />

var upload = uploads[i];<br />

}<br />

});<br />

In each case, the progress property of each operation will tell you how far the transfer has come<br />

along. The progress.status property is especially important. Again, status is a<br />

Background-TransferStatus value and will be one of idle, running, pausedByApplication,<br />

pausedCosted-Network, pausedNoNetwork, canceled, error, and completed). These are clearly necessary to<br />

inform the user, as appropriate, and to give her the ability to restart transfers that are paused or<br />

experienced an error, to pause running transfers, and to act on completed transfers.<br />

Speaking of which, when using the background transfer API, an app should always give the user<br />

control over pending transfers. Downloads can be paused through the DownloadOperation.pause<br />

method and resumed through DownloadOperation.resume. (There are no equivalents for uploads.)<br />

Download and upload operations are canceled by canceling the promises returned from startAsync.<br />

This brings up an interesting situation: if your app has been terminated and later restarted, how do<br />

you restart transfers that were paused? The answer is quite simple. By enumerating transfers through<br />

getCurrentDownloadsAsync and getCurrentUploadsAsync, incomplete transfers are automatically<br />

restarted. But then how do you get back to the promises originally returned by the startAsync<br />

methods? Those are not values that you can save in your app state and reload on startup, and yet you<br />

need them to be able to cancel those operations, if necessary, and also to attach your completed, error,<br />

and progress handlers.<br />

For this reason, both DownloadOperation and UploadOperation provide a method called attachAsync,<br />

which returns a promise for the operation just like startAsync did originally. You can then call the<br />

promise’s then or done methods to provide your handlers:<br />

promise = download.attachAsync().then(complete, error, progress);<br />

and call promise.cancel() if needed. In short, when Windows restarts a background transfer and<br />

essentially calls startAsync on your app’s behalf, it holds that promise internally. The attachAsync<br />

methods simply return that new promise.<br />

A final question is whether a suspended app can be notified when a transfer is complete, perhaps to<br />

issue a toast to inform the user. Such a feature isn’t supported in Windows 8 as there is no background<br />

task available for this purpose. At present, then, the user needs to switch back to the app to check on<br />

transfer progress.<br />

654

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

Saved successfully!

Ooh no, something went wrong!