Back

Saving Data

Hello World

In many cases you need to store user specific data like app settings on the users device.
DroidScript provides some functions which makes it easier to handle this.
Note: All explained methods are listed in the Database section.

Save data during one session

Some settings are just meaningful in a current running program.
For example if you keep track of the current app state (opened tabs, documents, pages).
For this purpose you can use the app.SetData and the app.GetData method.
To clear stored data use app.ClearData.

Save data across multiple app starts

Many apps support in-app settings which will be loaded after each app start. The app object has several methods to save and load different types of data.
To clear a saved value use the app.ClearValue method.

Save SaveBoolean SaveNumber SaveText
Load LoadBoolean LoadNumber LoadText
Type Boolean Number String

If you have many properties to save you can use an object structure instead of saving each value on their own.
Load settings: var conf = JSON.parse( app.LoadText( "settings", "{}" ));
Save settings: app.SaveText( "settings", JSON.stringify( conf ));

Example - App Settings

// default settings
var settings = { version: "1.0", startNo: 0 };

function LoadSettings() {
    var tmp = JSON.parse(app.LoadText( "settings", "{}" ));

    // update settings object
    for(var i in tmp) settings[i] = tmp[i];
}

function SaveSettings() {
    app.SaveText( "settings", JSON.stringify(settings) );
}


function OnStart() {
    LoadSettings();
    settings.startNo++;
    app.ShowPopup("Started " + settings.startNo + ". time");
    SaveSettings();
}
    Copy     Copy All       Run      

Using Databases

Using Databases is the most elaborate variant of the three.

You should only consider using one if you have to perform complex queries on a large amount of datasets.
Using JSON objects is more practicable and performant in most cases.

DroidScript provides an extra OpenDatabase component for databases which uses the SQLitePlugin cordova-sqlite-storage.

For a demo Have a look at the Database example.