These are chat archives for ReactiveX/RxJava

17th
Sep 2016
ViTORossonero
@ViTORossonero
Sep 17 2016 10:23

Hi, could anybody answer my question?
Given this code:

                .observeOn(Schedulers.newThread())   //newThread
                .doAfterTerminate(() -> { /* side effect code */ })
                .subscribe(
                        data -> { /* onNext code */ },
                        throwable -> { /* onError code */ }
                );

Are there any guaranties that side effect code and onNext|onError code will be executed sequantially on newThread without interruptions?
I mean that no other code can possibly be executed on this newThread between side effect code and onNext|onError code executions.

Dorus
@Dorus
Sep 17 2016 11:32
@ViTORossonero Well for one onNext code will run before side effect code
But yes, other than the order it does run sequantially.
And uninterrupted.
What other code are you afraid that would run and how would that mess things up?
ViTORossonero
@ViTORossonero
Sep 17 2016 14:26

thanks

@ViTORossonero Well for one onNext code will run before side effect code
yeah, I used doAfterTerminate by mistake, wanted to use doOnTerminate

ViTORossonero
@ViTORossonero
Sep 17 2016 14:50

I have some common code in onNext and in onError, so I wanted to move it to doOnTerminate(or doAfter), but this code has to be run uninterruptedly with code in onNext|onError. Because this common code changes some state. This state I'm checking up the stream on the same thread(in my code it's android main thread, and then I observeOn android main stream, not newThread).
So I think I really need to know that this code

.observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        data -> { 
                            /* common code */
                            /* onNext code */
                        },
                        throwable -> { 
                            /* common code */
                            /* onError code */
                        }
                );

is equal to this one

.observeOn(AndroidSchedulers.mainThread())
                .doOnTerminate(() -> { /* common code */ })
                .subscribe(
                        data -> { /* onNext code */ },
                        throwable -> { /* onError code */ }
                );

So there is no possibility that my code that is executed up the stream on the Android main thread will be executed after common code but before onNext|onError code

Dorus
@Dorus
Sep 17 2016 15:45
@ViTORossonero yeah doOnTerminate would run together with onCompleteor onError, so that's better. But next would still run before either of them.
Also next does not invoke doOnTerminate
You would need doOnEach
ViTORossonero
@ViTORossonero
Sep 17 2016 16:31
thanks, again
I messed up a little, in real code I'm actually using Single not Observable
but since Single doesn't have doOnTerminate will use doOnEach ignoring notificatios