Start a new topic

Get recently saved entities from AppData while offline? [SECOND TRY]

(Sorry for posting this again, but I can't update my first post.)



I followed the Android Caching and Offline Guide on the Kinvey website and now have something this in my MainActivity:



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



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

final AsyncAppData ad = mKinveyClient.appData("myCollection", MyEntity.class);

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

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



MyEntity event = new MyEntity();

event.setName("Launch Party");

event.setAddress("Kinvey HQ");



ad.save(event, new KinveyClientCallback() {

@Override

public void onFailure(Throwable e) {

Log.e("TAG", "failed to save event data", e);

}

@Override

public void onSuccess(MyEntity r) {

Log.d("TAG", "saved data for entity "+ r.getName());



ad.get(new KinveyListCallback() {

@Override

public void onSuccess(MyEntity[] result) {

Log.v("TAG", "received "+ result.length + " events");

}

@Override

public void onFailure(Throwable error) {

Log.e("TAG", "failed to fetch all", error);

}

});

}

});

}



Now if the phone is online and I do start the app serveral times I get the following output:



V/TAG﹕ received 0 events

V/TAG﹕ received 1 events

V/TAG﹕ received 2 events

V/TAG﹕ received 3 events

...



But if I activate the airplane mode to simulate offline usage:



V/TAG﹕ received 3 events

V/TAG﹕ received 3 events

V/TAG﹕ received 3 events

V/TAG﹕ received 3 events

...



As you can see the AppData won't get the recently saved entities while being offline. Do I get something wrong or is this kind of caching not possible?

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.
Thanks for your answer. I've read the explanation of "Updating the local store" but thought that a get all request wasn't technically a query and therefore it would be possible to do a get all on local offline storage. As this feature would be a big reason for me to choose Kinvey over other competitive MBaaS servies: Do you know how long it might take until an "offline get all" will be supported?
It can be in the next release, I think this is a good feature to have. I'd like to get it out this week, but it will be next week at the latest.
Hello Edward, do you have any news regarding the next update? I would be really happy to hear from you because at the moment I'm just waiting for the new offline feature ...
quick update-- it works, and I will be shipping the library in an hour or two. I'm also adding support for cancelling requests, and am currently testing that implementation. Once it's good to go I'll start the release process and post here again when it's live.
That's great to hear. Thanks!
hey check out version 2.6.13 of the android library, I added this feature.



Let me know if it works for you!



http://devcenter.kinvey.com/android/downloads
with OfflinePolicy.LOCAL_FIRST not getting data when device is offline, getting error "unable to resolve host bass.kinvey.com"
Login or Signup to post a comment