23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Finally, avoid using XHR for large file transfers because such operations will be suspended when the<br />

app is suspended. Use the Background Transfer API instead (see the next section), which uses XHR under<br />

the covers, so your web services won’t know the difference anyway!<br />

And on that note, let’s now look at that Background Transfer API in detail.<br />

Sidebar: Debugging Network Traffic with Fiddler<br />

If you’re interested in watching the HTTP(S) traffic between your computer and the<br />

Internet—something that can be invaluable when working with XmlHttpRequests—check out the<br />

freeware tool known as Fiddler (http://www.fiddler2.com/fiddler2/). In addition to inspecting<br />

traffic, you can also set breakpoints on various events and fiddle with (that is, modify) incoming<br />

and outgoing data. It supports traffic from any app or browser, including Windows Store apps.<br />

Background Transfer<br />

One need with user data, especially, is to transfer potentially large files to and from an online repository.<br />

For even moderately sized files, however, this presents a challenge: very few users typically want to stare<br />

at their display to watch file transfer progress, so it’s highly likely that they’ll switch to another app to do<br />

something far more interesting while the transfer is taking place. In doing so, the app that’s doing the<br />

transfer will be suspended in the background and possibly even terminated. This does not bode well for<br />

trying to complete such operations using a mechanism like WinJS.xhr!<br />

One solution would be to provide a background task for this purpose, which was a common request<br />

with early previews of Windows 8 before this API was ready. However, there’s little need to run app code<br />

for this common purpose. WinRT thus provides a specific background transfer API,<br />

Windows.Networking.BackgroundTransfer, supporting up to 500 scheduled transfers systemwide. It<br />

offers built-in cost awareness and resiliency to changes in connectivity, reliving apps from needing to<br />

worry about such concerns themselves. Transfers continue when an app is suspended and will be<br />

paused if the app is terminated. When the app is resumed or launched again, it can then check the<br />

status of background transfers it previously initiated and take further action as necessary—processing<br />

downloaded information, noting successful uploads in its UI, and enumerating pending transfers, which<br />

will restart any that were paused or otherwise interrupted. On the other hand, if the user directly closes<br />

the app (through a gesture, Alt+F4, or Task Manager), all pending transfers for that app are canceled.<br />

This is also true if you stop debugging an app in Visual Studio.<br />

Generally speaking, then, it’s highly recommended that you use the background transfer API<br />

whenever you expect the operation to exceed your customer’s tolerance for waiting. This clearly<br />

depends on the network’s connection speed, and whether you think the user will switch away from your<br />

app while such a transfer is taking place. For example, if you initiate a transfer operation but the user<br />

can continue to be productive (or entertained) in your app while that’s happening, then using WinJS.xhr<br />

with HTTP GET and POST/PUT might be a possibility, though you’ll still be responsible for cost<br />

643

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

Saved successfully!

Ooh no, something went wrong!