locked
Saving ItemList into LocalSettings

    Question

  • Hello,

    I just wanna save My Items in LocalSettings !! i wrote some code and it works fine ^^ ! in every checkpoint i clear my data and save it again :

     app.oncheckpoint = function (args) {
    
    
            var lenght = eventsData.itemList.length;
            var i = 0;
            //deleteExpiredItems();
            var list = new WinJS.UI.ListView(document.getElementById("eventsList"));
            var data = list.itemDataSource;
    
            appdata.clearAsync().done(
                function () {
                    while (i < lenght) {
                        var item = JSON.stringify(eventsData.itemList.getItem(i).data);
                        localdata.values[i] = item;
                        console.log(localdata.values[i]);
                        i++;
                    };
                     }
                );
            
         
            
    
            
    
            
           
        
    
        };

    But when the Apps is suspended i loose the whole data :s ! Can anyone help ?

    Thanks

     

    Monday, October 21, 2013 7:12 PM

Answers

  • By default, your app is suspended as soon as your return from the checkpoint handler. Because your call to clearAsync returns immediately (once it starts the operation), you'll never really get to execute your handler code before you're suspended.

    To accommodate these cases, there's a deferral mechanism that tells Windows to not suspend until your async operations are done (up to the usual time limit). What you need to do is pass a promise to args.detail.setPromise, preferably the promise that you'd get back from clearAsync.then (not done). That is:

    args.detail.setPromise(appdata.clearAsync().then( function () { while (i < lenght) { var item = JSON.stringify(eventsData.itemList.getItem(i).data); localdata.values[i] = item; console.log(localdata.values[i]); i++; }; } ));

    I talk about this mechanism is Chapter 3 of my book if you want to know more.

    Kraig

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

    Also see second edition preview


    Monday, October 21, 2013 8:20 PM

All replies

  • By default, your app is suspended as soon as your return from the checkpoint handler. Because your call to clearAsync returns immediately (once it starts the operation), you'll never really get to execute your handler code before you're suspended.

    To accommodate these cases, there's a deferral mechanism that tells Windows to not suspend until your async operations are done (up to the usual time limit). What you need to do is pass a promise to args.detail.setPromise, preferably the promise that you'd get back from clearAsync.then (not done). That is:

    args.detail.setPromise(appdata.clearAsync().then( function () { while (i < lenght) { var item = JSON.stringify(eventsData.itemList.getItem(i).data); localdata.values[i] = item; console.log(localdata.values[i]); i++; }; } ));

    I talk about this mechanism is Chapter 3 of my book if you want to know more.

    Kraig

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

    Also see second edition preview


    Monday, October 21, 2013 8:20 PM
  • One word :D WORKS GREAT ! THANKS MAN

    im reading your book !

    Tuesday, October 29, 2013 11:04 PM
  • Glad to hear it...hopefully you're reading the second preview of my second edition, which we put out just today. See http://aka.ms/BrockschmidtBook2.

    Tuesday, October 29, 2013 11:27 PM