D
D
Denis2019-10-08 06:23:35
Android
Denis, 2019-10-08 06:23:35

How to organize a synchronous sequence of commands on callbacks with RxJava2?

I'm picking RxJava2 a little, but I can't put together a set of disparate review articles into one picture. For the most part, the examples relate to incoming events of the same type from different emitters. Yes, RxJava2 is a nice thing for these tasks, but I still can’t figure out how to apply it for more complex tasks.
Actually, the question is the following:
Is it possible to conveniently solve the following problem using RxJava2, and if so, how?
There is a set of tasks that must be executed by the chain, the generation of each next task is carried out by a method that, based on the result of the execution of the tasks, can either generate the next link or complete the chain.
The task is executed as follows: either a method is launched that returns (or does not return) a callback in a given timeout, or it receives (or does not receive) a notification from another component (listener, eventbus). The entire generated chain is executed in a separate thread.
Actually, how to create an emitter or a disposable source that will generate new events (a new command) based on the results of the old one (the result of the execution is passed by a callback, for example, to the body of the closure).
As I understand it, Subjects are suitable for this, but some tricky manipulations with streams are needed with them, and in any case it is not obvious to me how to build this whole chain.
The best thing I came up with is just a thread, inside of which something similar to Iterable, at the start of each new task, the handler timeout is triggered and there is a PublishSubject, to which the manager of these commands is subscribed, and the timeout command or handler calls onNext of the subject with the result of execution . But, this is clearly not a reactive solution.
Specific task: communication with a ble device using NS Ble. Protocol sequences of commands, for example, the following sequence:
1) Read the characteristic, the answer comes in the callback.
2) Depending on what you read, send a package with one or another content to another characteristic.
3) Wait from the library according to the characteristic (notification) that the response will be sent by the device.
How to link RxJava API and this task (abstract)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tiroman, 2019-10-08
@tiroman

class Scratch {
    public static void main(String[] args) {
        Disposable disposable = getCharact1()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .flatMap(Scratch::getCharact2)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    //обрабатываем результат выполнения второй функции
                }, throwable -> {
                    //показываем сообщение об ошибке
                });
    }

    private static Single<Integer> getCharact1() {
        return Single.create(emitter -> {
            int result = 0;

            //выполняем какие-то действия

            if (!emitter.isDisposed()) {
                emitter.onSuccess(result);
            }
        });
    }

    private static Single<String> getCharact2(int result1) {
        return Single.create(emitter -> {
            String result = "";

            //выполняем какие-то действия

            if (!emitter.isDisposed()) {
                emitter.onSuccess(result);
            }
        });
    }
}

S
Sergey, 2019-10-08
@red-barbarian

Tips on what I understood from the question)
1. separate callbacks and rx. That is, make wrappers that return Observable/Single/... type (let's call it) RxB
2. at a more abstract level, work with RxB . standard merge, flatMap, etc. (standard Rx code)
i) It's better to use standard PublishRelay
items instead of PublishSubject. You can read almost all books. The main thing is to divide it into levels and then everything will become clear
1) callback -> rxjava
2) rxjava code
3) rxjava subscription in ui

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question