Search code examples
iosibm-mobilefirstjsonstore

Worklight 6.2 JSONStore issues on iOS


Since my update to WL6.2 i'm having issues with the JSONStore on iOS (7.1.1). There is a lot of unexpected behavior in my application. I have created a test app to indicate some of my issues. Just create a new WL project and replace the main.js file and add some buttons to the index.html.

index.html:

<button id="destroyJSONStore">Destroy JSON Store</button>
<button id="search">Search</button>
<button id="load">Load</button>

main.js:

function wlCommonInit(){
    document.getElementById("destroyJSONStore").onclick=function(){
        WL.JSONStore.destroy()
        .then(function() {
            alert("JSON Store Destroyed");
        });
    };

    document.getElementById("search").onclick=function(){
        var query = {UserID: 1};
        WL.JSONStore.get("Users").find(query)
        .then(function(res) {
            alert("Number of users found with ID 1: " + res.length);
        })

        .fail(function(errorObject) {
            alert("Error loading user: " + userId + "\n" + errorObject.msg);
        });
    };

    document.getElementById("load").onclick=function(){
        var data = [{UserID: 1, Login: 'hvb'}];
        var addOptions = {markDirty: true};

        WL.JSONStore.get("Users").add(data, addOptions)
        .then(function(added) {
            alert("User successfully added; " + added);
        })
        .fail(function (errorObject) {
            alert(errorObject.toString());
        });
    };

    var collections = {
        Persons : {
            searchFields : {name: 'string', age: 'integer'}
        }
    };

    var collections2 = {
        Users : {
            searchFields: { UserID: 'integer', Login: 'string'}
        }
    };

    var options = {
      username : 'jos',
      password : 'jos',
      localKeyGen : true
    };

    WL.JSONStore.init(collections, options)
    .then(function () {
        WL.Logger.debug("init persons ok");
    })
    .fail(function (errorObject) {
        alert(errorObject.toString());
    });

    WL.JSONStore.init(collections2, options)
    .then(function () {
        WL.Logger.debug("init Users ok");
    })
    .fail(function (errorObject) {
        alert(errorObject.toString());
    });           
}

The first time the app loads all is working fine, if you click load a new document is added in the store, if you click search the number of documents in the store is shown. However, when I start playing around with the app things go wrong. Close and open the app a couple of times and add new documents. After a while you'll notice the search keeps returning the same number of documents (regardless of how many times you click add).

You'll also notice that once you destroy the json store and reopen your application, you get a JSON_STORE_DATA_PROTECTION_KEY_FAILURE. You'll have to restart the application a second time to get rid of it.

Can someone please tell me what I'm doing wrong? I'll admit that initializing the JSONSstore in two times may seem a bit odd. But this is because our application is build with different modules, and regarding of the configuration different modules are loaded. According to the documentation this shouldn't cause any problems:

It is possible to initialize multiple times with different collections. New collections are initialized without affecting collections that are already initialized.

By the way: the app works as expected on Android and on the mobile browser simulator.


Solution

  • As suggested by Daniel, the problem is both init methods are being executed at the same time. This causes weird behavior. The solution is to call the second init after the first one is finished. Here is an example of how this can be done.

    WL.JSONStore.init(collections, options)
    .then(function () {
        WL.Logger.debug("init persons ok");
    })
    .then(function() {
        WL.JSONStore.init(collections2, options)
       .then(function () {
           WL.Logger.debug("init Users ok");
       })
       .fail(function (errorObject) {
           alert(errorObject.toString());
       });
    })
    .fail(function (errorObject) {
        alert(errorObject.toString());
    });