0

I'd like to read the value of a property

var checkInEnabled: Driver<Bool> { get }

I only need to to run a bit code once when the class has loaded, so I don't want to use something like:

roomStatus.checkInEnabled
      .drive { [weak self] enabled in
        if !enabled {
          // do something everytime it changes
        }
      }.disposed(by: disposeBag)

But rather something like this:

if roomStatus.checkInEnabled {
//only do something now
}

Thanks for reading,

Code
  • 81
  • 10
  • 1
    As I understand it, the `Driver` is a sequence. If nothing has been emitted by that sequence then it doesn't have a value to read. It sounds like you are trying to do something that is fighting against the Reactive model. Whatever you are trying to accomplish with your `if` check should be done in response to the `Driver` emitting a value. – Scott Thompson Oct 12 '21 at 17:52
  • 1
    @ScottThompson is correct. A `Driver` doesn't store state, it emits values. There is no Bool inside your driver to query. – Daniel T. Oct 12 '21 at 20:37
  • If you explained better exactly what you are trying to do, it may help. Is `checkInEnabled` in the same class as the one where you want to run the code? – Daniel T. Oct 13 '21 at 11:36

1 Answers1

0

use take(1) operator

roomStatus.checkInEnabled
      .take(1)
      .drive { [weak self] enabled in
        if !enabled {
           // ~~~
        }
      }.disposed(by: disposeBag)
SEUNGHWAN LEE
  • 204
  • 1
  • 4