Search code examples
cordovapersistencelocal-storagepersistlawnchair

Phonegap: can't make localStorage persist


So I init everything:

var Store = new Lawnchair({table:'mightapp', adaptor:'gears'}, 
    function(s) {
    var obj = {msg: 'hooray!'};

    s.save({key: "msg", value: obj});
});

Then I comment out the message to test if it persisted...

var Store = new Lawnchair({table:'mightapp', adaptor:'gears'}, 
    function(s) {
    //var obj = {msg: 'hooray!'};

    //s.save({key: "msg", value: obj});
});

And it doesn't -- what am I doing wrong? I'm making a fitness app and I need to persist people's workouts/workout stats pretty much permanently... should I just do some file i/o?


Solution

  • dom adapter works fine for me. Here is the code I am using to persist data across application restart.

    <script type="text/javascript">
        var store = new Lawnchair({
            adapter: "dom",
            name: "testing"
        }, function(store) {
        });
    
        store.exists('dhaval', function(available){
            var preStr = "";
    
            // check whether required data is available, if not create it
            if(available){
                preStr = "data is already available, ";
            }else{
                preStr = "data is not available, ";
                var me = {
                    key: 'dhaval'
                };
    
                // save it
                store.save(me);
            }
    
            // access it later... even after app restart!
            store.get('dhaval', function(me) {
                $("#data").html(preStr + JSON.stringify(me));
            });
        });
    </script>
    

    For full source check git repo android-cordova-lawnchair