Search code examples
androidcachingandroid-ion

Android : How to use Cache in ion library?


In my case, I want to load data from Cache. If there is cache load from cache else load from network. How can I use caching in Ion?

    private void loadION() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("loading");
    progressDialog.show();
    Ion.with(getApplicationContext()).load(url)
            .setBodyParameter("tag", "annual_schedule").asString()
            .setCallback(new FutureCallback<String>() {
                @Override
                public void onCompleted(Exception e, String str) {
                    Message.Toast(getApplicationContext(), str);
                    progressDialog.dismiss();
                }
            });
}

Solution

  • Ion will automatically cache GET requests. This is a POST request, and can not be cached by the library.

    Furthermore, cached requests can't be used right away, since disk I/O is still treated as a blocking call. It will still be an asynchronous request.