Using caching and offline storage in android
- 2021-12-21 05:14:28
- OfStack
1. Using caching and offline storage in android
Caching can speed up your application, even when the network is unavailable, and users can use your application more smoothly. Using caching is quite simple, requiring a single line of code.
Import
import com.shephertz.app42.paas.sdk.android.App42CacheManager
You can, and you need to set the cache policy.
Policy.CACHE_FIRSTSetting
The read operation that activates all data is first obtained from the cache. If the data in the cache is available and not invalid, it will be returned directly from the cache. Otherwise, the network requests this data and updates or adds this data to the cache at the same time. You can set the cache expiration period through API, and the default is 11 hours.
Policy.NETWORK_FIRST
First get data from the network, and then update the cache. If the network is unavailable, the data is taken out of the cache
cache.Policy.NOCACHEBy
This is
App42 SDK
By default, no caching is used and data is always read only from the network.
Set the cache policy as follows:
App42CacheManager.setPolicy(Policy.CACHE_FIRST);
Cache expiration period:
App42CacheManager.setExpiryInMinutes(<EXPIRY_TIME_IN_MINUTES>);
The case code is as follows:
UserService userService = App42API.buildUserService();
String userName = "Nick";
userService.getUser(userName,new App42CallBack() {
public void onSuccess(Object response)
{
User user = (User)response;
if(user.isFromCache()){
//Response coming from Cache
System.out.println("userName is " + user.getUserName());
System.out.println("emailId is " + user.getEmail());
System.out.println("is from cache is " + user.isFromCache());
}
else{
//Response From Server
System.out.println("userName is " + user.getUserName());
System.out.println("emailId is " + user.getEmail());
}
}
public void onException(Exception ex)
{
System.out.println("Exception Message"+ex.getMessage());
}
});
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.
If the response comes from the cache, you pass the
isFromCache
Identified as
true
So that you can tell whether the data is real-time or from the cache.
The following is what needs to be added in manifest. xml:
<receiver android:name="com.shephertz.app42.paas.sdk.android.App42BroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name="com.shephertz.app42.paas.sdk.android.AlarmReceiver"/>
<service android:name="com.shephertz.app42.paas.sdk.android.App42DataSyncService"/>
2. Offline storage offline storage
Offline storage allows you to submit data when the local network is unavailable, and when the network is available, the server will synchronize. This is very useful in many situations, such as if your user plays a game and achieves 1 certain level of completion. However, when the score is sent, the network is down, so his score may be lost. Using offline cache will save his score if the local network is unavailable, and will synchronize with the server's later when the network is restored to availability.
Use offline:
App42API.setofflineStorage(true);
Case code:
//Set Offline Storage to True
App42API.setofflineStorage(true);
String gameName = "<Enter_your_game/level_name>";
String userName = "Nick";
BigDecimal gameScore = new BigDecimal(3500);
scoreBoardService.saveUserScore(gameName, userName, gameScore,new App42CallBack() {
public void onSuccess(Object response)
{
Game game = (Game)response;
if(game.isOfflineSync())
{ //Request is saved in cache
System.out.println("Information is Stored in cache, will send to App42 when network is available");
}
else {
//Response Received From Server and is Succeseful
System.out.println("Server Response : " + game);
}
}
public void onException(Exception ex)
{
System.out.println("Exception Message"+ex.getMessage());
}
});