locked
How to save temporary data to the file at the time of suspending(sleeping)

    Question

  • I would like to save the temporary data to the file when suspending event fired on Javascript WinRT application. However, if the pause of this application is quick, async methods(such as createFileAsync etc.) are not called and it doesn't save data.

    I would like to certainly save these data before an app becomes a suspended state. Isn't there any good idea?

    Tuesday, July 30, 2013 8:54 AM

Answers

  • WinJS.Application.oncheckpoint wraps suspending. The eventArgs object has a setPromise method to which you can pass a promise representing your async work (e.g. the promise from a single async call or one from the last .then call in a chain). It ties directly into the deferral mechanism.

    In the example above, within the checkpoint even you'd call eventArgs.setPromise(WinJS.Application.local.writeText(...)); If you need your own completed handler, attach it with then instead of done, as then returns a promise that's completed when your completed handler returns. By passing that second promise to setPromise, you'll link the deferral to your completed handler.

    Wednesday, July 31, 2013 5:01 AM
  • Clearly PhoneGap has some definition of a "task" that you construct to pass to this function, and then it plugs into the setPromise functionality. You can also just add the checkpoint event listener yourself, or listen to the suspending event independently. They'll all work. In any case, you know the mechanism for deferrals now, so should be also to delay suspending until your async work is done.
    Wednesday, July 31, 2013 3:00 PM

All replies

  • If you do any async work within the suspending event handler, you need to use a "deferral" so that Windows knows to delay suspending your app until you call the deferral's complete (although you still have the time limit). Specifically, the eventArgs object given to the

    Windows.UI.WebUI.WebUIApplication.onsuspending eventArgs.suspendingOperation contains an instance of Windows.UI.WebUI.WebUIApplication.SuspendingOperation. This provides a getDeferral method that returns a deferral object. You call that object's complete method inside the completed handler for your async operation (or at the end of a promise chain) to let Windows know that you can now be suspended. Here's a bit of an example

    var suspendingDeferral = eventObject.suspendingOperation.getDeferral();
    WinJS.Application.local.writeText("myfile.txt", document.getElementById("userText").value).done(function () {
        suspendingDeferral.complete();
    });
    

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview



    Tuesday, July 30, 2013 4:53 PM
  • Also make sure you save data (as opposed to state) earlier and more often than when suspending. It is possible for the user to kill the app without suspending it. If the app relies on suspending to save data then that data can be easily lost.

    --Rob

    Tuesday, July 30, 2013 6:06 PM
    Moderator
  • It's useful. But it seems that I cannot use it because PhongeGap uses a checkpoint event.
    Wednesday, July 31, 2013 4:55 AM
  • WinJS.Application.oncheckpoint wraps suspending. The eventArgs object has a setPromise method to which you can pass a promise representing your async work (e.g. the promise from a single async call or one from the last .then call in a chain). It ties directly into the deferral mechanism.

    In the example above, within the checkpoint even you'd call eventArgs.setPromise(WinJS.Application.local.writeText(...)); If you need your own completed handler, attach it with then instead of done, as then returns a promise that's completed when your completed handler returns. By passing that second promise to setPromise, you'll link the deferral to your completed handler.

    Wednesday, July 31, 2013 5:01 AM
  • Thank you for the detailed explanation. It seems that all functions are executed in this code.

    function setPauseTask(task) {
        WinJS.Application.addEventListener("checkpoint", function (e) {
            e.detail.setPromise(new WinJS.Promise(task));
        }, false);
    }

    Wednesday, July 31, 2013 5:50 AM
  • Clearly PhoneGap has some definition of a "task" that you construct to pass to this function, and then it plugs into the setPromise functionality. You can also just add the checkpoint event listener yourself, or listen to the suspending event independently. They'll all work. In any case, you know the mechanism for deferrals now, so should be also to delay suspending until your async work is done.
    Wednesday, July 31, 2013 3:00 PM