0

I'm a freshman in the RxSwift's world.Here is my circumstance: First of all,I have an Enum defines two states of my business. Then,I have 2 buttons,Tap button A, first I want to change the value of Enum to stateA, then send a request. Tap button B will change the value of Enum to stateB, and will send a different request. I would like to use with RxSwift.Appreciate your help.

Frank8888
  • 11
  • 2

1 Answers1

0

So you have two causes:

  1. User taps button A
  2. User taps button B

And you have three effects:

  1. Send request A
  2. Send request B
  3. Update state enum

That means you will have three Observable chains... Leading from a cause or causes to an effect.

buttonA.rx.tap
    .bind(onNext: { 
        api.sendRequestA()
    })

buttonB.rx.tap
    .bind(onNext: { 
        api.sendRequestB()
    })

let state = Observable.merge( 
    buttonA.rx.tap.map { Enum.a },
    buttonB.rx.tap.map { Enum.b }
)
Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • Hi,Daniel!Thank you for your help.In your pseudocode I see you separate it into two steps:Sending request and altering state.But Is there a way to make the two step in a chain?Which means when user taps ButtonA/ButtonB, we modify the state firstly, then some observable(s) is observing the state, once the state modified, send a request based on the current state automatically.Thank you very much. – Frank8888 May 22 '23 at 01:40
  • There's no need to do that, and doing so would break Separation of Concerns. Don't break Separation of Concerns. – Daniel T. May 22 '23 at 17:26