solidcell solidcell - 4 months ago 9
Swift Question

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

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

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