Mark
05/03/2019, 3:45 AMfun myFun(): LiveData<MyClass>
to a suspend function that returns as soon as the LiveData value is set (i.e. the first time since LiveData created)? Note - the actual scenario is Room where I sometimes want LiveData<List> and othertimes just want the List of results, but don’t want to specify the SQL annotation twice. The best I have so far is:
suspend fun mySuspendFun() = suspendCoroutine<List<MyClass>> { cont ->
val live = myFun()
live.observeForever(object: Observer<List<MyClass>> {
override fun onChanged(it: List<MyClass>) {
live.removeObserver(this)
cont.resume(it)
}
})
}
withContext(Dispatchers.Main) {}
around the suspendCoroutine
call because observeForever
must be called on main thread