ios - Observable 不接收事件

标签 ios observable rx-swift object-lifetime combinelatest

我的代码是这样的:

 private let myPublishSubject = PublishSubject<Bool>()

 func method(input: String) -> String {
     if input == something {
         myPublishSubject.onNext(true)
         return update(input)
     }
     myPublishSubject.onNext(false)
     return input
 }

 // Output
 let driver: Driver<String>

 init() {
     let myObservable = Observable.just("a")
     driver = Observable.combineLatest(myObservable, myPublishSubject)
         .map { 
...
         }
         .asDriver()
 }

现在,在调试中,我发现 driver 只是订阅和处理。当 method 之后被调用时,myPublishSubject.onNext 没有任何订阅者。 我该如何解决?

最佳答案

@Aks 昨天回答正确:PublishSubject 事件一到就流式传输,无需等待任何订阅者。因此,当事件被触发并且未订阅驱动程序时,我丢失了该事件。 因此,我改用了 BehaviorRelay,它解决了我的问题当订阅 BehaviorRelay 时,即使事件发生在订阅者创建之前,你也会得到最后一个流。

关于ios - Observable 不接收事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289674/

相关文章:

ios - CarPlay 与外部配件的关系和无线配件配置

ios - Google Analytics(分析)Flash跟踪是否可以在IOS打包的空中应用程序中使用?

Angular4 可观察到数组

Swift 3、RxAlamofire 和映射到自定义对象

ios - 从现有的 NSString 构建 unicode 字符串

node.js - Observable.fromPromise 仅在 CircleCI 上构建时不存在

java - 除了 javafx.beans.property 提供的机制之外,我是否应该在非图形用户界面应用程序逻辑中使用其他东西?

ios - UITableViewCell 中的 Reactive<UISegmentedControl>

rx-swift - rxswift 绑定(bind)(onNext : VS subscribe(onNext:

ios - UIImagePickerController 每秒显示一次黑屏