Caching

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:

Cache Policies
  • Policy.CACHE_FIRST

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.

  • Policy.NETWORK_FIRST

This policy enables data to be fetched from network and then updates cache. If network is not available, data is fetched from cache.

  • Policy.NOCACHE

By default App42 SDK uses this policy and does not use any cache and always reads data from network only.

Setting Cache Policy

To set policy, you have to import com.shephertz.app42.paas.sdk.android.App42CacheManager class and set required Policy as shown below.

	App42CacheManager.setPolicy(Policy.CACHE_FIRST); 

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.

	App42CacheManager.setExpiryInMinutes(<EXPIRY_TIME_IN_MINUTES>); 
Sample Code
		
		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.

Changes in Manifest File

Following changes 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"/>
Offline storage

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.
	
	App42API.setofflineStorage(true);
  • You have to handle response whether response coming locally or from server as shown below.
//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());
}
});

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.