Is there a preferred type of Observable without the need for a value in the following events?

I have an Observable which is only used to run flatMap / map . Therefore, I only need the Next event and never value. I could use my own concept for that basket value, but I wonder if there is an RxSwift convention for it.

Here is what I mean:

 // 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 } 

In this example, triggeringObservable has rx_observer for some property in the library that tells us that username() ready to be called.

+9
swift rx-swift reactivex
source share
5 answers

You can simply use Observable<Void> for this purpose. For example:

  let triggerObservable = Observable<Void>.just() triggerObservable.subscribeNext() { debugPrint("received notification!") }.addDisposableTo(disposeBag) 

or in your example:

 let triggeringObservable: Observable<Void> // ... triggeringObservable.map { Void -> String in // The actual value is ignored return SomeLibrary.username() } 
+10
source share

In Swift 4 (and possibly earlier), you can pass an empty tuple as an argument to a method that expects a Void-related type.

 var triggerObservable = PublishSubject<Void>() ... triggerObservable.onNext(()) // "()" empty tuple is accepted for a Void argument 

Or you can define an extension to overload onNext () with no arguments for the Void case:

 extension ObserverType where E == Void { public func onNext() { onNext(()) } } ... triggerObservable.onNext() 
+3
source share

This worked for me on Swift 4.1:

 Observable<Void>.just(Void()) 
+2
source share

With fast 4, Xcode will say that you cannot invoke "simply" without arguments. we are currently using the following workaround:

  Observable.just(Void.self).subscribe(onNext: { Void -> String in debugPrint("triggered!") }).addDisposableTo(disposeBag) 
0
source share

This worked for me on Swift 4.2:

 Observable<Void>.just(()) 
0
source share

All Articles