Rxswift: How to use rx_tap to alter Observable<Bool>

Created on 3 Mar 2016  路  2Comments  路  Source: ReactiveX/RxSwift

I'm trying to toggle a Observable by a rx_tap signal.

I tried to use flatMap, but that either doesn't work or i'm using it incorrectly.

boolValue.flatMap { value -> value in
    return !value
}

Any idea?

Most helpful comment

Hi guys,

there is also a scan operator, so something like:

button.rx_tap
     .scan(false) { state, _ in 
          !state
     }
     .startWith(false)

... should work.

You can also take a look at calculator example in RxExample app for further details.

All 2 comments

Hi @coryoso
Well, I think your rx_tap should be reflection of your viewModel behavior

Kind of

let flag: Variable<Bool>

button.rx_tap.map{ !flag.value }.bindTo(flag).addDisposable(disposeBag)

So in this case you will bind your UI triggers to viewModel

and then bind viewModel back to UI

Hi guys,

there is also a scan operator, so something like:

button.rx_tap
     .scan(false) { state, _ in 
          !state
     }
     .startWith(false)

... should work.

You can also take a look at calculator example in RxExample app for further details.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trungp picture trungp  路  3Comments

dmial picture dmial  路  3Comments

acecilia picture acecilia  路  3Comments

hannesstruss picture hannesstruss  路  3Comments

RobinFalko picture RobinFalko  路  3Comments