Sergio C.
03/22/2022, 5:29 PMcallbackFlow { }
but the code inside the block never gets called, am I doing something wrong?Joffrey
03/22/2022, 5:31 PMcallbackFlow
, we should be able to answer this 😉ephemient
03/22/2022, 5:34 PMflow()
and channelFlow()
are cold flows, they only run when collectedSergio C.
03/22/2022, 5:45 PMfun getSensors(): LiveData<ReadResult> {
return sensorsRepository.getSensorsFLow().asLiveData()
}
in the repository
private var data: Flow<ReadResult> = MutableSharedFlow()
init {
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
val dataStore: DataStore<AppPreferences> = app.appPreferencesStore
dataStore.data.catch {
updateObservers(AppPreferences.getDefaultInstance())
}.collectLatest {
updateObservers(it)
}
}
}
private fun updateObservers(appPreferences: AppPreferences) {
stopSensors()
when (appPreferences.selectedDevice) {
AppPreferences.Device.SUNI -> initSuniReader()
AppPreferences.Device.TZ702 -> initTZReader()
AppPreferences.Device.NEXPEK7 -> initNexpekReader()
else -> flowOf(ReadResul())
}
}
private fun initNexpek(): Flow<ReadResult> {
return callbackFlow<ReadResult> {
init sensor and wait for callback
trySend(the result)
}
}
private var data: MutableSharedFlow<ReadResult> = MutableSharedFlow()
and everyone sends data to it with tryEmitJoffrey
03/22/2022, 5:48 PMawaitClose()
call at the end of the callbackFlow
blockcallbackFlow
is meant for multi-shot callbacks. If you want to encapsulate a single-shot callback you should make a suspend
function returning a single value instead of a flow, and you can build that function using suspendCancellableCoroutine
. I'm not sure which one is your case thoughSergio C.
03/22/2022, 5:49 PMawaitClose()
inside the lock?Joffrey
03/22/2022, 5:51 PMawaitClose { }
block. Check the documentation of callbackFlow
Sergio C.
03/22/2022, 6:10 PMephemient
03/22/2022, 6:20 PMSergio C.
03/22/2022, 11:39 PMgildor
03/23/2022, 1:50 AM