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.