Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use caching and offline storage in android

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use caching and offline storage in android. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Use caching and offline storage in android

Caching can speed up your application, and even when the network is unavailable, it is fairly simple for users to use your application more smoothly, requiring a single line of code.

You can import import com.shephertz.app42.paas.sdk.android.App42CacheManager, and you need to set the caching policy.

Policy.CACHE_FIRSTSetting will activate the read operation of all data to be obtained from the cache first. If the data in the cache is available and does not expire, it will be returned directly from the cache. Otherwise, the data will be requested on the network, and the data will be updated or added to the cache at the same time. You can set the cache expiration period through API. The default is 1 hour.

Policy.NETWORK_FIRST first fetches data from the network and then updates the cache. If the network is not available, the data is taken out of the cache

Cache.Policy.NOCACHEBy this is the default of App42 SDK, does not use any cache, and always reads data only from the network.

Set the caching policy as follows:

App42CacheManager.setPolicy (Policy.CACHE_FIRST)

Cache expiration period:

App42CacheManager.setExpiryInMinutes ()

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 identify it as true in the response via isFromCache, so you can tell whether the data is in real time or from the cache.

The following needs to be added in manifest.xml:

2. Offline storage offline storage

Offline storage allows you to submit data unavailable on your local network, and the server synchronizes when the network is available. This is very useful in many cases, such as if your users play games and achieve certain levels of completion. However, when sending the score, the network is down, then his score may be lost, and using offline cache will save his score if the local network is not available, and will synchronize with the server later when the network is available.

Use offline:

App42API.setofflineStorage (true)

Case code:

/ / Set Offline Storage to TrueApp42API.setofflineStorage (true); String gameName = ""; 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 ();}}) So much for sharing about how to use caching and offline storage in android. I hope the above can help you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report