Question

Environment: Worklight Studio 6.1 on Windows. I want to confirm our understanding of using the JSONStore.init() method. This is following on from this question that establishes that when calling init() to reopen a store we must pass the exact same parameters as when we init-ed the store for the first time.

The question now is exactly when we should be calling init(), and whether we can cause any problems by accidentally calling init() multiple times. This may seem obvious but we are trying to diagnose some error conditions and one possible cause is that we might be calling init() too often.

My expectation is that the requirement is that in any given execution of an application using JSONstore collections must call init() for each collection (perhaps by calling init() with a list of collections) and that a reasonable model is to is to do that at application startup. This is based on my reading of the docs JSONstore.init.

Please could we confirm that from that point on we can :

a). use methods such as JSONStore.get() freely with no further init() calls being needed.

b). should we invoke init() again for an already initialised collection there should be no side effects.

The symptom we appear to have is that sometimes a second call to init() results in an error message

 PROVISION_TABLE_SEARCH_FIELDS_MISMATCH  in worklight.js at line 4556

although so far as we can see we always call init() using indentical collection parameters.

Was it helpful?

Solution

You only need to call init if you are opening a collection for the first time, or reopening it after closing the store. You only want to close the store if you are done using JSONStore and want to close it for security purposes, or if you want to switch to another JSONStore user if you are using multiple users. If you have no need for these two situations, you do not have to close the store.

You can definitely init the collection(s) at startup and have them open for the rest of the app's lifecycle, and then close it when the app closes, for example. You do not have to call init when you call get, or any other JSONStore method; you only have to call init, as i said, when you are using the collection for the first time, or after you closed or destroyed the store (by calling WL.JSONStore.closeAll() or WL.JSONStore.destroy()).

As for the error you are getting, it most likely means that you are passing different fields the second time you call init, and you cannot do that, since search fields cannot be changed dynamically. If you want to change the search fields, you first have to remove that collection (and lose the data inside it), and then reinitialize it with the new data.

To verify that you can call init multiple times (with the same arguments), you can run the following sample code:

var collections = {
          people : {
            searchFields : {name: 'string'}
          },
          orders : {
            searchFields: {name: 'string'}
          }
        };

        WL.JSONStore.init(collections)

        .then(function () {
          return WL.JSONStore.init(collections);
        })

        .then(function () {
          return WL.JSONStore.init(collections);
        })

        .then(function () {
          //init called 3 times succesfully
            alert('Multiple inits worked');
        })

        .fail(function (err) {
          //this should not be called
            alert('Multiple inits failed' + err.toString());
        }); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top