ahulyk
07/24/2019, 9:11 AMgildor
07/24/2019, 9:14 AMgildor
07/24/2019, 9:15 AMahulyk
07/24/2019, 9:15 AMgildor
07/24/2019, 9:16 AMtseisel
07/24/2019, 9:19 AMConflatedBroadcastChannel
is the closest thing to LiveData
. You could create a LiveData
subclass that wraps a ConflatedBroadcastChannel
.
You can also use the liveData
function from androidx.lifecycle:lifecycle-livedata-ktx
: https://developer.android.com/topic/libraries/architecture/coroutines#livedatagildor
07/24/2019, 9:20 AMYou could create aI would rather create adaptersubclass that wraps aLiveData
ConflatedBroadcastChannel
LiveData.asFlow()
and Flow.asLiveData()
than use inheritanceJoaquim Ley
07/24/2019, 9:24 AMahulyk
07/24/2019, 9:31 AMsuspend fun <T> BroadcastChannel<T>.asLiveData(): LiveData<T> {
val liveData = MutableLiveData<T>()
consumeEach { value ->
liveData.postValue(value)
}
return liveData
}
this shoud work - will test it latergildor
07/24/2019, 9:33 AMgildor
07/24/2019, 9:33 AMahulyk
07/24/2019, 10:22 AMgildor
07/24/2019, 10:59 AMfun <T> BroadcastChannel<T>.consumeAsLiveData(scope: CoroutineScope): LiveData<T> {
val liveData = MutableLiveData<T>()
scope.launch {
consumeEach {
liveData.postValue(it)
}
}
return liveData
}
gildor
07/24/2019, 10:59 AMZach Klippenstein (he/him) [MOD]
07/24/2019, 6:08 PMliveData
coroutine builder function that you can use for this too:
https://developer.android.com/topic/libraries/architecture/coroutines#livedata
fun <T> BroadcastChannel<T>.consumeAsLiveData(scope: CoroutineScope): LiveData<T> = liveData {
consumeEach {
emit(it)
}
}