Is there a preferred type for an Observable with no need for a value in Next events?

solidcell picture solidcell · Apr 11, 2016 · Viewed 9.2k times · Source

I have an Observable which is only used for triggering flatMap/map. So I only ever need the Next event and never a value. I could use my own concept for such a trash value, but I'm wondering if there's an RxSwift convention for it.

Here's what I'm dealing with:

// 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 is rx_observer on some property in the library which will let us know that username() is ready to be called.

Answer

Greg Ferreri picture Greg Ferreri · Apr 12, 2016

You can simply use an Observable<Void> for this purpose. Like so:

   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() 
}