Caching with Retrofit — Store responses offline
Modifying your Retrofit to make your app offline-friendly without a database…
Caching is a way of temporarily storing data fetched from a network on a device’s storage, so that we can access it at a later time when the device is offline or if we want to access the same data again.
In this article, I’ll be talking about how to enable caching with Retrofit in your app, and store responses temporarily for later use. All this without using a database! 😎
For brevity purposes, I won’t be going into the many aspects of caching in detail. This article will be practical and quick guide on how to enable caching in your new or existing Android app that uses Retrofit.
But before that, why should you even have caching in your app?
Benefits of Caching
- Reduces bandwidth consumption.
- Saves you time you’d spend waiting for the server to give you the network response.
- Saves the server the burden of additional traffic.
- If you need to access the same network resource again after having accessed it recently, your device won’t need to make a request to the server; it’ll get the cached response instead.
So without further ado, let’s get started with the practical part of this article.
How you’ve been using Retrofit so far…
If you’ve been using Retrofit, let’s say with GSON, your
Retrofit instance would look something like this:
The problem with this is that it uses the default
OkHttpClient as a client to execute your requests. And this isn’t very “cache-friendly”.
We’ll be creating an instance of the
OkHttpClient that is cache-enabled and can handle fetching the data efficiently when:
- the device is offline, and
- the device needs to access the same data from the network within a short span of time.
Creating a cache-friendly OkHttpClient
Step 1: Define a method to check for internet connectivity
We first need to have a method in our app that checks for Internet connectivity. This is boilerplate code but here it is, in case you’re already familiar with it and want to take a look at it again:
Step 2: Defining the size of the cache
The following line of code specifies a cache of 5MB. Note that it needs to be a
val cacheSize = (5 x 1024 x 1024).toLong()
Step 3: Creating the Cache variable
We pass in the cache directory and the cache size as parameters to create the
Cache variable for our
OkHttpClient. Also note here that we need the context in order to do this.
val myCache = Cache(context.cacheDir, cacheSize)
Step 4: Creating the OkHttpClient instance with an Interceptor
We need to add an
Interceptor, which is responsible for observing and modifying requests going out and the corresponding responses coming back in.
The following gist explains how to do add this to our
OkHttpClient, along with adding our cache:
To read more about the
only-if-cached attributes we’ve used above, you can visit this link:
The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and…developer.mozilla.org
Putting it all together…
Now we need to add this newly-created
OkHttpClient to our
Retrofit instance. Here’s how to do that:
Here’s how your code should look like after you’re done with all the steps I’ve gone through above:
If the network API you’re using for your app has a limit on the number of requests you can make, then not only is caching with Retrofit a helpful strategy to apply, but also something that is essential for you to keep working with the API. So I hope this article has given you insight into how you can leverage the advantage of caching with Retrofit and make your app more offline-friendly without using a database.
Plus, this is super useful when you want your users to access the data (response) from the same request when their device is offline too!
Until next time! ✌️
If you liked this article, click on the 👏 button (did you know you could go up to 50?), follow me here on Medium and share this article on your socials!
You can also find me and all my socials here:
A Google Certified Android Developer who loves making apps, watchfaces & memes.bapspatil.com
Check out all the top articles at blog.mindorks.com