Question

I followed this 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<MyEntity> ad = mKinveyClient.appData("myCollection", MyEntity.class);
    ad.setCache(new InMemoryLRUCache(), CachePolicy.CACHEFIRST);
    ad.setOffline(OfflinePolicy.LOCAL_FIRST, new SqlLiteOfflineStore<MyEntity>(this));

    MyEntity event = new MyEntity();
    event.setName("Launch Party");
    event.setAddress("Kinvey HQ");

    ad.save(event, new KinveyClientCallback<MyEntity>() {
        @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<MyEntity>() {
                @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 several 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 beeing offline. Do I get something wrong or is this kind of caching not possible?

Was it helpful?

Solution

I'm an engineer working on the Android library at Kinvey and can help you out with this one.

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<WeightEntity>() { 
    public void onSuccess(WeightEntity result){
        myweights.get(new KinveyListCallback<WeightEntity>() { 
            ... 
        }); 

    }
    ... 
}); 

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.

Let me know if that makes sense!

----------------------------------After edit:

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top