Recently, while studying the official Google GithubBrowserSample project, I found that Yigit Boyar had written an Adapter in the project to connect Retrofit with LiveData. But this practice has not been officially sanctioned by Retrofit since around 2017 for the following reasons:

  1. JakeWharton thinks it’s strange to wrap Http requests as LiveData wrappers
  2. LiveData was new, and apis were still unstable
  3. Retrofit’s extension mechanism gives developers a lot of room to customize various integrations, and it’s not a good place to write things down

As The Android Architecture Components get better and better, more and more people are switching to LiveData and enjoying the benefits of the MVVM pattern. Considering that the date was 2017, with JakeWharton himself joining Google and android architecture components already released, With this already in the official Google Demo, I thought it was time to give it a try.

I pulled out a library of code from the official Demo, and when integrated it made it easy to use Retrofit with LiveData.

The project address

Github.com/shawnlinboy…

reference

implementation 'me. Linshen. Retrofit2: adapter - livedata: 1.0.0'
Copy the code

usage

Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory())
            .build()
            .create(GithubService::class.java)
            .getUser("shawnlinboy").observe(this,
                Observer { response ->
                    when (response) {
                        is ApiSuccessResponse -> {
                          //success response
                        }
                        else- > {//failed response}}})Copy the code

instructions

The biggest “problem” with combining Retrofit with LiveData is that LiveData itself is a wrapper for data classes or an Observable without the concept of Callback, so it needs an ApiResponse to carry it. So I’ll put the Body in, so the outermost layer will always be the ApiResponse when it’s used.

In fact, to be honest, after moving the library and writing the Demo, I think this approach is much easier than combining with RxJava. Although I still do not deny that RxJava is an excellent library, and I still rely on RxJava for scheduling in many areas of my current projects, I still feel that RxJava is too unfriendly for beginners, mainly due to the high cost of getting started: On the one hand, you need to change your programming thinking to the responsive direction. On the other hand, the excessive number of operators in RxJava really caused many friends in my team to drink a lot. They often used the wrong operator, and some even found that there was a problem with the original operator after the launch, so they should change to another one.

I will try to combine Retrofit with LiveData in the next few projects to see if there are any stuck scenarios. If you have a better way to write it or a better idea, you are welcome to submit it. Welcome to PR.