Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava an Retrofit Network call subscribe

I am trying to understand the retrofit with RxJava. I have seen many different examples on subscribe method and couldn't find the proper explanation for doing it.

1st One

 Observable<PostMessage> call =  service.callAPI(data);
        call.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PostMessage>(

                ));

2nd One

Observable<PostMessage> call =   service.callAPI(data);
call.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<PostMessage>(

        ) {
            @Override
            public void accept(PostMessage postMessage) throws Exception {

            }
        });

}

3rd One

Observable<PostMessage> call =   service.callAPI(data);
call.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new DisposableObserver<PostMessage>() {
            @Override
            public void onNext(PostMessage postMessage) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

}

4th One

Observable<PostMessage> call =  service.callAPI(data);
        call.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PostMessage>(

                ) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PostMessage postMessage) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

Could anyone explains what there these three ways of doing it. Each has different meaning or do the same thing ?

like image 954
R.Roshan Avatar asked Jan 06 '18 08:01

R.Roshan


People also ask

How do you use RxJava with retrofit?

To use RxJava in retrofit environment we need to do just two major changes: Add the RxJava in Retrofit Builder. Use Observable type in the interface instead of Call.

What is retrofit RxJava?

Retrofit is a HTTP Client for Android and Java developed by Square. We are going to integrate Retrofit with RxJava to simplify threading in our app. In addition, we will also integrate RxAndroid to make network calls.


1 Answers

1:a Schedulers.io() is intended for input-output bound work and it's another thread doing its job except the fact that they are cached and recycled for another job if in future if any comes.

1:b AndroidSchedulers.mainThread() because you wanto receive the results back on main thread.

1:c new Subscriber Subscriber is subscribed to Flowable and is another implementation of the Observer.

2: new Consumer A functional interface (callback) that accepts a single value.

3: new DisposableObserver is also a Observer but abstract and allows asynchronous cancellation by implementing Disposable.

4: new Observer Observer is subscibed to Observable and is Provides a mechanism for receiving push-based notifications. when Observable will call the onCompleted() and onNext() or OnError() when finished and only once.

The main difference from Observable is that new Subsciber supports backpressure while both works almost the same also Subscriber is an implementation of the Observer.

And the main difference between Subscriber and Consumer is both are as follows

Observer/Observable: The watching thread is observed by the controller. In case of an event happening, the controller is then notified and can assign the new task to a free thread from a reusable cached thread pool (or wait and cache the tasks in FIFO queue if all threads are currently busy). The worker threads implement Callable and either return successfull with the result (or a boolean value), or return with an error, in which case the controller may decide what to to (depending on the nature of error that has happended).

Producer/Consumer: The watching thread shares a BlockingQueue with the controller (event-queue) and the controller shares two with all workers (task-queue and result-queue). In case of an event, the watching thread puts a task object in the event-queue. The controller takes new tasks from the event-queue, reviews them and puts them in the task-queue. Each worker waits for new tasks and takes/consumes them from the task-queue (first come first served, managed by the queue itself), putting the results or errors back into the result-queue. Finally, the controller can retrieve the results from the result-queue and take according steps in case of errors.

sources:

https://softwareengineering.stackexchange.com/questions/286763/difference-between-consumer-producer-and-observer-observable

What is the difference between an Observer and a Subscriber?

http://reactivex.io/RxJava/javadoc/rx/schedulers/Schedulers.html

http://reactivex.io/RxJava/javadoc/io/reactivex/functions/Consumer.html

http://reactivex.io/RxJava/javadoc/io/reactivex/observers/DisposableObserver.html

like image 99
vikas kumar Avatar answered Oct 04 '22 04:10

vikas kumar