Auto Caching with Retrofit
If you’re an android developer and you cache your objects then there’s a chance you’ve written code like this:
And that’s the pretty version.
You end up writing each database query manually and as your code grows so does the manual work.
Wouldn’t it be nice to treat caching like a switch? Just say which objects you want cached and be done with it?
That’s what we’re gonna explore today!
The idea is simple. Save every response annotated with
@Cacheable directly into the database as a POJO, using Retrofit’s converter factory.
When building your Retrofit instance you have the option to add a converter factory, like so:
The key line here is:
if (listener != null && isCacheable) listener.onCacheableResponse(clsType, response);
Whenever we get the response body we check if the
@Cacheable annotation is present
and notify the listener with the response and response class type.
Lastly we need to setup
GsonConverterFactory.class to include our listener
How to use
There are 2 steps involved:
- Simply annotate the calls you want to cache with
@Cacheable, like so:
2. Use the listener to cache the response:
Now you have a single point where all http responses are saved directly into your database.
Checkout the sample project for a full code example
- We used gson but we could as easily use any other converter as long as we add a listener to the response converter.
- No need to use ObjectBox. Any storing mechanism flexible enough will work