I have a Single from RxJava and want to continue working with a Deferred from Kotlin Coroutines. How to accomplish that?
fun convert(data: rx.Single<String>): kotlinx.coroutines.Deferred<String> = ...
I would be interested in some library (if there is any?) as well as in doing this on my own... So far I did this hand-made implementation on my own:
private fun waitForRxJavaResult(resultSingle: Single<String>): String? {
var resultReceived = false
var result: String? = null
resultSingle.subscribe({
result = it
resultReceived = true
}, {
resultReceived = true
if (!(it is NoSuchElementException))
it.printStackTrace()
})
while (!resultReceived)
Thread.sleep(20)
return result
}