Search code examples
androidretrofitrx-javarx-java2

Which rxjava3 retrofit-adapter should we use for Rxjava3


I am using RxJava3 and retrofit but I am unable to get a rxjava3 retrofit-adapter for RxJava3.


Solution

  • There is now an official Retrofit implementation with version 2.9.0:

    Just use the adapter where you create your Retrofit client:

    val rxAdapter = RxJava3CallAdapterFactory.create()
    retrofit = Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .client(httpClient)
                .addCallAdapterFactory(rxAdapter).build()
    

    And include the RxAdapter dependency in your build.gradle:

    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
    

    https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-290-2020-05-20

    Also from the documentation:

    Unlike the RxJava 1 and RxJava 2 adapters, the RxJava 3 adapter's create() method will produce asynchronous HTTP requests by default. For synchronous requests use createSynchronous() and for synchronous on a scheduler use createWithScheduler(..)