Search code examples
androidcachingretrofit2okhttpoffline-caching

Data caching in Retrofit2


I did a project with retrofit2 and it works well. Project has 2 parts and each part gets data(Json object) from server with retrofit2. Now, I want to add cache system. I searched but I didn't find a good example. I have data which are object, string, integer(not image). I guess okhttp3 is best cache for my project. Can you explain best way for caching and can you share a good example. My plan:

  • I will get a cache version from server with retrofit2.
  • I will check version. If it is updated, it will show cached data. If not, it will get new data from server.
  • Also, it will work offline.

Solution

  • I used TinyDB for caching in android.