Start a new topic

Retrieve ID of locally saved entity in Cache store

Hi,


I'm writing a small app to test out Kinvey.


Given a device is offline and the app is using a Cache datastore, when I call datastore.save(entity), the returned promise rejects. However I can see in the browser's local DB that the entity has been successfully stored locally and an ID generated from it.


Is there a way I can retrieve the generated ID of the entity so my app can continue to interact with it whilst the application is offline?


Thanks,


James


James,


If you implement the suggestions given in my earlier comment, then you will notice that it returns the entities from cache with "_id" property along with other properties.


If you just want to retrieve the "id" value of the entities, then you will have to use Field Selection explained on this link. You will have to include all the properties while updating an entity. Saving entities after retrieving them using Field Selection will result in the loss of all fields not selected. Currently, there is no way to pass only the attributes you want to be changed. The backend stores the entire JSON body as passed in the request body.


I request you to implement all the method to get a better understanding.


Thanks,

Pranav

Kinvey

Thanks Pranav. I just want to retrieve the ID of an entity that has been saved locally but not yet to the backend. It seems as though it cannot be done using the cache data store. James

James,


If you have wanted to fetch ALL the entities from the cache, you can use the following code snippet:


    var stream = dataStore.find();
    stream.subscribe(function onNext(entities) {
      // ...
    }, function onError(error) {
      // ...
    }, function onComplete() {
      // ...
    });



If you don't want to fetch all the entities but a few, then you will have to use a 'query'. To fetch multiple entities using a query, call dataStore.find and pass in a query as follows:


    var query = new Kinvey.Query();
    query.equalTo('field', 'value');
    var stream = dataStore.find(query);
    stream.subscribe(function onNext(entities) {
      // ...
    }, function onError(error) {
      // ...
    }, function onComplete() {
      // ...
    });



For more explanation about Kinvey Querying, please check this link.



Thanks,

Pranav

Kinvey

Hi Pranav, Thanks for your reply. I'm using the HTML SDK v3.10. I think I understand all the points you mentioned. My question is, given an entity has been stored locally but not to the backend, how can I retrieve its _id property so that my app can continue to interact with it while the device is still offline? Thanks, James

Hello James,


I am not sure which Kinvey SDK you are using. Thanks for confirming that you are using Cache datastore. You use Cache datastore when you want the data stored on the device to optimize your app’s performance and/or provide offline support for short periods of network loss. This is the default DataStore type if a type is not explicitly set.


Please check this link and the example is given on it. Following are a few points which will provide you with a better understanding of the concept:

  • When you call 'dataStore.pull()' method, it pulls the data from the backend and saves it to the cache.
  • When you call 'dataStore.find()' method, the function will be called twice. 1st call will search the entities in the cache and the next call will search it on the backend.
  • When you call 'dataStore.save()' method, the entity will first be saved to cache and then to the backend. If you do not have a network connection, the entity will be stored locally. You will need to push it to the server when network reconnects, using 'dataStore.push()' method.


If you have "_id" value of the entity then you can use 'dataStore.findById('entity-id')' method as mentioned on this link.



Thanks,

Pranav

Kinvey

Login or Signup to post a comment