swift - 是否有一个 Observable 的首选类型而不需要在 Next 事件中有一个值?

标签 swift rx-swift reactivex

我有一个仅用于触发 flatMap/map 的 Observable。所以我只需要 Next 事件而不需要值。我可以使用我自己的概念来表示这样一个垃圾值,但我想知道是否有针对它的 RxSwift 约定。

这是我正在处理的:

// I'd rather not have an Element type that someone might use
let triggeringObservable: Observable<SomeSessionClass> 

// ...

triggeringObservable.map { _ -> String in // The actual value is ignored
    return SomeLibrary.username() // `username()` is only ready when `triggeringObservable` sends Next
}

在这个例子中,triggeringObservable 是库中某些属性的 rx_observer,这会让我们知道 username() 已准备好打电话。

最佳答案

您可以简单地使用 Observable<Void>以此目的。像这样:

   let triggerObservable = Observable<Void>.just()

    triggerObservable.subscribeNext() {
        debugPrint("received notification!")
    }.addDisposableTo(disposeBag)

或者在你的例子中:

let triggeringObservable: Observable<Void> 

// ...

triggeringObservable.map { Void -> String in // The actual value is ignored
   return SomeLibrary.username() 
}

关于swift - 是否有一个 Observable 的首选类型而不需要在 Next 事件中有一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557510/

相关文章:

python - 如何等待 RxPy 并行线程完成

ios - 如何使用 ReactiveX 按顺序执行异步

ios - 如何设置 UNNotificationRequest 自定义重复间隔?

ios - Firebase -Auth.auth().currentUser?.phoneNumber 返回 nil

swift - 在 Swift 3 中搜索 Realm 关系

swift - RxSwift Driver 第一次调用两次

ios - 使用 RxSwift 的 collectionView 不显示任何项目

ios - Apple HealthKit - 后台更新未触发

ios - 如何使用 RxSwift 将数据添加到 tableview 或 collectionview

java - 连续执行不同的Completables