Reputation: 2650
Below initialize() method was working great, but it has to work on the same io thread (ioScheduler) as the other calls.
fun initialize(): Single<Boolean> {
return Single.create { callback ->
callback.onSuccess(true)
}
}
I've tried changing it to this code:
internal fun initialize(): Single<Boolean> {
return Single.create<Any> { singleEmitter ->
singleEmitter.onSuccess(true)
}
.observeOn(ioScheduler)
.subscribe() as Single<Boolean>
}
But that creates a new problem:
ConsumerSingleObserver cannot be cast to io.reactivex.Single
I've also tried this:
Completable.fromAction { someMethod() }
.subscribeOn(ioScheduler)
.subscribe()
But that doesn't return the boolean value.
Upvotes: 0
Views: 2654
Reputation: 69997
This works for me:
internal fun initialize(): Single<Boolean> {
return Single.create<Boolean> { singleEmitter ->
singleEmitter.onSuccess(true)
}
.observeOn(Schedulers.io())
}
The output is boolean so you should specify <Boolean>
as the exact type parameter to create
. In addition, you should simply return the chain and not subscribe to it.
Upvotes: 2
Reputation: 57
If you are calling the method from UI thread, than you will not receive the Boolean, as you have subscribed Completable on IO thread, you should observe it on UI thread explicitly
Upvotes: -1