RxJava. Sequential execution

后端 未结 4 1335
旧巷少年郎
旧巷少年郎 2021-02-05 11:44

In my Android App I have a presenter which handles user interactions, contains kind of request manager and if needed sends user input over request manager to request manager.

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 12:24

    My solutions would be as follows (I did something similar in Swift before):

    1. You will need a wrapper interface (let's call it "Event") for both requests and responses.
    2. You will need a state object (let's make it class "State") that will contain request queue and the latest server response, and a method that will accept "Event" as parameter and return 'this'.
    3. Your main processing chain will look like Observable state = Observable.merge(serverResponsesMappedToEventObservable, requestsMappedToEventObservable).scan(new State(), (state, event) -> { state.apply(event) })
    4. Both parameters of the .merge() method will probably be Subjects.
    5. Queue processing will happen in the only method of "State" object (pick and send request from the queue on any event, add to queue on request event, update latest response on response event).

提交回复
热议问题