Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 549
Chetan Gaikwad Avatar asked Mar 09 '20 19:03

Chetan Gaikwad


People also ask

What are retrofit call adapter used for?

Retrofit uses an HTTP client (OkHttp) to execute the network requests. This execution happens on a background thread. When OkHttp client receives a response from the server, it passes the response back to Retrofit.

What is RxJava2CallAdapterFactory?

Class RxJava2CallAdapterFactoryA call adapter which uses RxJava 2 for creating observables. Adding this class to Retrofit allows you to return an Observable , Flowable , Single , Completable or Maybe from service methods.


1 Answers

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(..)

like image 170
devz Avatar answered Sep 27 '22 22:09

devz