0
votes

I'm using Rxjava with Retrofit. I know how to use it but I'm still not clear about "what Rxjava exactly does with Retrofit".


we use retrofit with any converter(like Gson) and Okhttp library for REST API call, once the response is got, it's of Observable type(if using Rxjava otherwise it'll be of CALL type). Now the thing is if we already got the response what exactly Rxjava doing with that response?


As far as I've got so far by looking into the codes that, Rxjava firstly takes that Observable response and does some operation using operators on that already gotten response and then secondly it just sends the final response to observer from where one can inflate the data into views.


  1. Firstly enlighten me if I'm wrong anywhere with my understanding of Rxjava.
  2. What is the need of doing operations on already gotten data as we've already got the data filtered using API queries using retrofit
  3. Why do we use Customer interface object in Rxjava? and so we use it at place of Observer or onNext().
  4. Does Rxjava also help in retrieving purpose or it just operates on already gotten data?
2

2 Answers

2
votes

RXJava is helping you with threading and converting into your existing reactive code.

It uses Flowable/Observable/Single to fetch your Data (for example if you have many of them in a Stream) it allows you to have backpressure using Flowable.

It also allows you to use the basic Reactive stuff like having the request in a different Thread then your observing.

That also means that you can have your Results/Requests as reactive stream and map/convert/... and whatever possible with RxJava.

You dont have to worry about that its a wrapper about your existing data, since the data are fetched inside an Interceptor and converted to use with RxJava.

You wont get any performance issues or have any issues at all since it's well tested by the community.

1) Firstly enlighten me if I'm wrong anywhere with my understanding of Rxjava.

RxJava stands for reactive-developing. It uses a common "coding-style" which is based on functional programming.

2) What is the need of doing operations on already gotten data as we've already got the data filtered using API queries using retrofit

You may want to have several operations on just one response. Which means that you can convert it to different "behaviours" or just merge/concat/zip/filter again. In some cases you are not able to control the api and need to process it later which means that reactive can help you in this task.

*3) Why do we use Customer interface object in Rxjava? and so we use it at place of Observer or onNext(). *

Since you request data you need to consume it. Its like having a list and you want to modify entries in the list it allows you to do that before you consume (output) it. There's also a consumer if an error occured.

4) Does Rxjava also help in retrieving purpose or it just operates on already gotten data?

Since it's an Interceptor it converts "existing" data into a reactive stream. It operates on already gotten data. At least in this case.

Example (Kotlin):

    var service = Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread()))
            .addCallAdapterFactory(ObserveOnMainCallAdapterFactory(AndroidSchedulers.mainThread()))
            .addConverterFactory(GsonConverterFactory.create(GsonUtil.gson()))
            .client(okHttpClient)
            .baseUrl(host!!)
            .build()
            .create(YourService::class.java)

service.getLotsOfData()
       .flatMapIterable{ it }
       .flatMap{ it.id = id+"whatEverModified" }
       .subscribeOn(Schedulers.io)
       .observeOn(AndroidSchedulers.mainThread())

 interface YourService { @GET("lots/of/data") fun getLotsOfData(): Flowable<CustomModel> }

Example (Java):

 YourService service = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(GsonUtil.gson()))
            .client(okHttpClient)
            .baseUrl(host)
            .build()
            .create(YourService.class);

service.getLotsOfData()
       .flatMapIterable(data -> data)
       .flatMap( data -> { data = data +"whatEverModified"; return data; })
       .subscribeOn(Schedulers.io)
       .observeOn(AndroidSchedulers.mainThread());

 interface YourService { @GET("lots/of/data") Flowable<CustomModel> getLotsOfData();  }

This code get your List of data, iteratable through each item, appends "whatEverModified" to each id in your Model and do all the work in the IO Thread. After all data are modified and fetched it returns the data in the Main Thread. Subscribe = Process data, Observe = Consumed data.

1
votes

For a reason why you should use RxJava with Retrofit, I'll point you to a previous answer of mine. Setting up Retrofit is an one-time cost (both at design and at runtime), but the benefits of conciseness and composability pay off forever.

Note that I think you're looking at RxJava at the wrong abstraction level - don't think in terms of Observer-Subscriber-Event; think more at the level of datastream with lazy/repeatable operations.