DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Applying Caching and Offline Storage in App42 Android SDK

Applying Caching and Offline Storage in App42 Android SDK

Offline caching lets users use your application even if the network is not available. Learn about caching and how to enable it.

Vicky Tambule user avatar by
Vicky Tambule
·
May. 30, 17 · News
Like (11)
Save
Tweet
Share
3.60K Views

Join the DZone community and get the full member experience.

Join For Free

We are excited to announce a new feature of offline caching and storage in App42 Android SDK. This will enable your application to be responsive even if the network is not available, so the user can use your application flawlessly without any trouble. Using this feature is quite straightforward and requires a single line of code to enable.

Below are the different policies and mechanisms of this feature.

Offline Caching

Offline caching enables your application to read data from the local cache if the network is not available. There are different cache levels that can be set using API and are explained below.

Cache Policies

1) Policy.CACHE_FIRST – Setting this policy will enable all read data to be first looked into the cache if data is available and not expired, then it will return from the cache; otherwise, network request will be made and the cache will be updated. You can set the cache expiry time by using an API, as explained below. By default, cache expiry is set to an hour.

// Setup caching policy

import com.shephertz.app42.paas.sdk.android.App42CacheManager;
import com.shephertz.app42.paas.sdk.android.App42CacheManager.Policy;

App42API.initialize(this, "<YOUR_API_KEY>", "<YOUR_SECRET_KEY>");
App42CacheManager.setPolicy(Policy.CACHE_FIRST); ; 
//Same can be done for network policy

//Set Cache Expiry time. Applicable only for CACHE_FIRST
App42CacheManager.setExpiryInMinutes("");

2) Policy.NETWORK_FIRST – This policy enables data to be fetched from the network and then update the cache. If the network is not available, data is fetched from the cache.

3) Policy.NOCACHE – By default, App42 SDK uses this policy and does not use any cache and always reads data from network only.

Offline storage

Offline storage allows you to post data locally in case the network is not available, and syncs it with the server later when the network is available. This is useful in many scenarios, e.g. if your user is playing a game and has scored something upon completion of a specific level. However, at the time of posting the score, if the network is not available, his score might get lost and his efforts will go to 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 the server when network availability resumes.

To use this feature, you just have to set offline storage to true, as shown below.

App42API.setofflineStorage(true) // just set offline storage true
scoreBoardService.saveUserScore(gameName, userName, gameScore,new App42CallBack() {
public void onSuccess(Object response)
{
  Game game = (Game)response;
  if(game.isOfflineSync())
   {
    System.out.println("Information is Stored in cache, will send to App42 when network is available");
   }
  else
   {
    System.out.println("Game Name is : "+game.getName());
     for(int i = 0;i<game.getScoreList().size();i++)
      {
       System.out.println("userName is : " + game.getScoreList().get(i).getUserName());
       System.out.println("score is : " + game.getScoreList().get(i).getValue());
       System.out.println("scoreId is : " + game.getScoreList().get(i).getScoreId());
       System.out.println("Created On is :"+game.getScoreList().get(i).getCreatedOn());
      }
    }
}
public void onException(Exception ex)
{
 System.out.println("Exception Message"+ex.getMessage());
}
});

Isn’t this simple and straightforward? To get started, sign up here and grab our new Android SDK to give a better user experience to your app users.

Software development kit Cache (computing) Android SDK Android (robot) Network

Published at DZone with permission of Vicky Tambule. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • 5 Challenges in Building Distributed Systems
  • Essential Mobile App Security Tips for Coders in 2023: Make Your App Unhackable
  • Debugging Streams and Collections

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: