A
A
Artem2017-06-24 21:11:33
Java
Artem, 2017-06-24 21:11:33

How to implement a request queue with Rx?

My application has an asynchronous makeRequest() method with a callback. This method is called many times from different classes. It is necessary that the execution of the contents of this method occurs sequentially (while the first request is being executed, the rest are waiting).
It would be desirable to implement it by means of Rx. In my opinion, it should look something like this:

public void execute() { // Этот метод вызывается многократно из разных классов
    Observable.just(true)
        // Что здесь нужно добавить для ожидания?
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .map(o -> {
                    internalExecute();
                    return o;
            })
        .subscribe();
}

private void internalExecute() { // Этот метод должен запускаться, только когда предыдущий запуск финишировал
    makeRequest(this::onRequestFinished);
}

private void onRequestFinished() {
    // Здесь обрабатывается завершений запроса
}

With this code, queries are executed in parallel. What needs to be added so that requests are sequential one after another?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question