Caching will enable your application to be responsive even if the network is not available and user can use your application flawlessly without any trouble. Using this feature is quite straight forward and requires a single line of code to enable it.
Offline caching enables your application to read data from local cache if network is not available. There are different cache levels that can be set using API and have been explained below:
Setting this policy will enable all read data to be first looked in to cache if data is available and not expired then it will return from cache otherwise network request will be made and cache will be updated for the same. You can set cache expiry time by using API as explained below. By default cache expiry is set to an hour.
This policy enables data to be fetched from network and then updates cache. If network is not available, data is fetched from cache.
By default App42 SDK uses this policy and does not use any cache and always reads data from network only.
To set policy, you have to import com.shephertz.app42.paas.sdk.android.App42CacheManager class and set required Policy as shown below.
You can also set cache expiry means when to update cache, If you have set network policy then cache will be updated every time record is fetched however if cache policy is set as CACHE_FIRST then cache will be updated after every one hour , you can also set custom cache expiry in minute as shown below.
If Response is from cache you will get isFromCache flag to true in the response so you can identify that data is real time or data is coming from cache.
Following changes to be added in manifest.xml
Offline storage allows you to post data locally in case network is not available and syncs it with server later when network is available. This is useful in many scenarios e.g. if your user is playing a game and has scored something on completion of a specific level. However, at the time of posting of score if network is not available his score might get lost and his efforts will go waste. This can be avoided using offline caching where his score will be saved locally in case of network unavailability and will be later synced with server when network availability resumes.
Offline Storage can be set in your app as shown below.
You have to handle response whether response coming locally or from server as shown below.
The expected behaviour of above code is that if network is available it give response from server and if network is not available response comes locally.