Start a new topic

Get recently saved entities from AppData while offline?

I followed the "Android: Caching and Offline" guide on the Kinvey website and now have something like this (within onCreate):





final Client mKinveyClient = new Client.Builder(this.getApplicationContext()).build();



AsyncAppData myweights = mKinveyClient.appData("weights" , WeightEntity.class);



myweights.setCache(new InMemoryLRUCache(), CachePolicy.CACHEFIRST);

myweights.setOffline(OfflinePolicy.LOCAL_FIRST, new SqlLiteOfflineStore(this));



WeightEntity weight = new WeightEntity();

weight.setWeightKg(75);



myweights.save(weight, new KinveyClientCallback() {

...

});



myweights.get(new KinveyListCallback() {

...

});





If I'm online I do get the latest entered weight. But if I try it offline (airplane mode on) then I'm not able to get the latest entry out of the AppData.





How can I get recently (offline) saved entities from AppData while beeing offline?

This is happening because of a timing issue, as our library is executing requests asynchronously. This means that after you call save, the save does not occur immediately, instead a thread is spawned and the save request is executed in the background. Once execution has completed, you will get the onSuccess callback (or the onFailure, if something goes wrong).



So, you have a couple of options on how to handle this, and the most common solution is to just wait for the onSuccess callback of that first save--



myweights.save(weight, new KinveyClientCallback() {

public void onSuccess(WeightEntity result){

myweights.get(new KinveyListCallback() {

...

});



}

...

});



With the above snippet, the save request will be executed, and once that is complete then the get will be executed. If you have a ton of embedded calls like this it can get unmanageable, so our library also has synchronous (blocking) implementations of all methods-- and you can write your own AsyncTask that can chain together a lot of blocking implementations.
OK, I see what's happening here-- the issue is that a Get request with no query (a get all, what you are using above) is still technically a query, and offline doesn't have support for resolving queries on the client. A query can be resolved on the server, and then the results are saved locally, so if the query is repeated while offline it will only return the previous results. I also tried to explain this here:



http://devcenter.kinvey.com/android/guides/caching-offline#Updatingthelocalstore



BUT



a get all request, while technically a query, can be resolved on the client by just returning every row that is stored offline. So, I have added a new ticket to the backlog, and will add support for this in the future! Thanks for your patience, and for pointing out that this can be supported.
Login or Signup to post a comment