Offline caching in Android

Yusuf Gültaç
3 min readJul 11, 2021

In this article, you will improve the user experience for an app by learning offline caching.

Nowadays a great amount of mobile aplications collaborate with data from the internet. Developers should always address the factors that negatively affect performance in applications that work with the Internet.

For instance your users may want to use the app while their device is offline, or if they have a lack of network connection. If your app fetches data from the server on every launch, the user might see a loading screen, and this might be a bad user experience. Users might uninstall your app.

When users launch an app, they expect the app to show data quickly even they have a internet problem. Developer can solve this problem by implementing offline caching. Offline caching means that your app saves data fetched from the network on the device’s local storage, for faster access. Many users have intermittent access to the internet. By implementing offline caching, you add offline support to your app, helping these users to use your app while they are offline.

Caching Techniques

So let’s look at some network caching techniques in android summarily.

1- Shared Preferences

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Good solution for simple requests and responses, infrequent network calls, or small datasets.

2- Internal storage directory

You can access the app’s internal storage directory and save data files in it. Your app’s package name specifies the app’s internal storage directory, which is in a special location in the Android file system. This directory is private to your app, and it is cleared when your app is uninstalled. It means that the other applications cannot reach your file which you identified your app.

Good solution if you have specific needs that a file system can solve. For example, if you need to save media files or data files and you have to manage the files yourself. You can’t use this technique to store complex and structured data.

3- Retrofit

Retrofit is a networking library used to implement a type-safe REST client for Android. You can configure Retrofit to store a copy of every network result locally. Good solution for simple requests and responses, infrequent network calls, or small datasets.

4- Room(Recommended)

You can cache data using Room, which is an SQLite object-mapping library that provides an abstraction layer over SQLite. This solution for complex and structured data, because the best way to store structured data on a device’s file system is in a local SQLite database.

Key concept: Don’t retrieve data from the network every time the app is launched. Instead, display data that you fetch from the database. This technique decreases app-loading time.

https://developer.android.com/codelabs/kotlin-android-training-repository#4

Conclusion

Each method may have its own usage requirements. Do not hesitate to use whichever is suitable for you. Room is one step ahead of the others because it offers a wide area of use. Don’t forget that the customer is always right so we should serve the best for them.

I would like to thank Google who helped me understand this topic.

References

This article was created with support from these sites.

https://developer.android.com/

https://developer.android.com/courses/kotlin-android-fundamentals/overview

--

--