Caching and Offline

Caching

Kinvey's Java library provides you with means to utilize caching. This feature can make your app more responsive, and allows you to handle various concerns around network connectivity on your java platform of choice.

Caching is integrated directly into the AppData API, so any time data is retrieved from Kinvey, the response can optionally be stored locally. You can use a cache policy to determine how AppData should behave when caching data and retrieving it while online.

Cache Policies

  • CachePolicy.NOCACHE - This policy will not use any caching, and will execute every request online.

Use this policy if your application is dependant on data that is shared between multiple users and always needs to be up to date.

  • CachePolicy.CACHEONLY - This policy will only retrieve data from the cache, and will not use any network connection.

Use this policy in combination with another policy, to allow for quick response times without requiring a network connection for specific operations.

  • CachePolicy.CACHEFIRST - This policy will first attempt to retrieve data from the cache. If the data has been cached, it will be returned. If the data does not exist in the cache, the data will be retrieved from Kinvey's Backend and the cache will be updated.

Use this policy if your application can display data that doesn't change very often but you still want local updates.

  • CachePolicy.CACHEFIRST_NOREFRESH - This policy will first attempt to retrieve data from the cache. If the data has been cached, it will be returned. If the data does not exist in the cache, the data will be retrieved from Kinvey's Backend but the cache will not be updated with the new results.

Use this policy if you want to set default results, however if a request is made that cannot return these defaults a live request will be made (without modifying those default values)

  • CachePolicy.NETWORKFIRST - This policy will execute the request on the network, and will store the result in the cache. If the online execution fails, the results will be pulled from the cache.

Use this policy if you application wants the latest data but you still want responsiveness if a connection is lost

  • CachePolicy.BOTH - This policy will first retrieve an element from the cache, and then it will attempt to execute the request on line. This caching policy will make two calls to the KinveyClientCallback, either onSuccess or onFailure for both executing on the cache as well as executing online.

Use this policy if you want more responsiveness without sacrificing the consistency of data with your backend.

Set Up AppData Cache

By default, AppData caching policy is CachePolicy.NOCACHE. Setting the cache policy to anything else requires only one line of code.

import com.kinvey.java.cache.InMemoryLRUCache;
import com.kinvey.java.cache.CachePolicy;

//...

AppData<EventEntity> myevents = kinveyClient.get("events", EventEntity.class);
myevents.setCache(new InMemoryLRUCache(), CachePolicy.NETWORKFIRST); //Add caching
//make request
try{
    EventEntity[] results = myEvents.getBlocking().execute();
    System.out.println("got " + results.length + " results back!");
}catch (IOException e){
    System.out.println("Couldn't load! -> " + e);
    e.printStackTrace();
}

The expected behavior of the above code will be a call to Kinvey data service to retrieve any data. If the network is unavailable a call to the cache will be made and returned instead.

Offline AppData

Offline AppData is not currently supported by the native Java Library. Check out our Android library or iOS library.

Got a question?