0
votes

I use RxJava in my project and I have a situation where 2 methods are called one after the other and both return void. Each of these methods internally use RxJava.

Pseudo:

void sendMsg_1() {
...
//Fetch data from DB using RxJava to send message to client.
..
}

void sendMsg_2() {
...
Uses RxJava to send message to client.
..
}

Invoking code:

sendMsg_1();
sendMsg_2();

Practically, sendMsg_2 is faster and client gets it before sendMsg_1 sends his msg. This is not good for me and I would like output of Msg1 be sent before Msg2.

How to do it?

Should I artificially return dummy observable just so I can use .flatMap as follow:

sendMsg_1()
.flatMap(msgObj-> { 
    return sendMsg_2();
}).subscribe();

Is there a better way?

Thank you!

1

1 Answers

0
votes

This is the right way to emit void methods the rxJava's way:

public rx.Completable func_A() {
    return Completable.create(subscriber -> {
        // func_A logic
        if(ok) subscriber.onCompleted();
        else subscriber.onError(throwable);
    });
}

func_A()
.doOnCompleted(() -> func_B())
.subscribe();

If it helps anyone...enjoy :-)