这是我的代码的简化版本:
var myObservable: Observable<MyEnum>
var modelObservable: Observable<Model?>
myObservable = myButton.rx.tap.asSignal()
.asObservable()
.flatMapLatest {
getModel()
}.map { model in
print("this is called")
return model.prop == true ? MyEnum.first : MyEnum.second
}
func getModel() -> Observable<Model?> {
if let model = self.model.value {
return Observable.just(model)
}
createModel()
return modelObservable
}
myObservable.subscribe(onNext: { (enum) in
switch enum {
case .first:
self.presentFirst()
case .second:
self.presentSecond()
}
}).disposed(by: bag)
我期望这段代码意味着每当点击myButton时,该代码便会运行并打印“此被调用”,但是,即使未点击myButton也会触发myOtherObservable时也打印“此被调用”。为什么会这样?这让我觉得我不了解Rx。另外,我将如何使其行为使其仅在轻按myButton时运行?