I have the following class, which exposes a Single to fetch an ID.
This is used to fetch the ID on init of that class and later others can also use this to get the ID.
Any where else from where I subscribe to this Single, I am able to get the ID. But in the init method here, this method doesn't seem to start and fetch the ID.
Could someone please help me understand what I am missing? Thanks
class TestClass {
val idObservable: Single<String>
get() {
return Single.create {
fetchId(it)
}
}
init {
idObservable
.subscribe { success, _ ->
Log.d(TAG, "Id: $success")
}
.dispose()
}
private fun fetchId(emitter: SingleEmitter<String>) {
Utils.fetchId(Consumer<String>{
emitter.onSuccess(it)
}, Consumer<Throwable>{
emitter.onSuccess("")
})
}
}
The reason why it was not working in the constructor init, was because the single was being disposed right away.
init {
idObservable
.subscribe { success, _ ->
Log.d(TAG, "Id: $success")
}
}
This above code works.