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)
}
}Sergio C.
03/22/2022, 5:46 PMSergio C.
03/22/2022, 5:47 PMprivate 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 blockJoffrey
03/22/2022, 5:49 PMcallbackFlow 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?Sergio C.
03/22/2022, 5:50 PMSergio C.
03/22/2022, 5:51 PMSergio C.
03/22/2022, 5:51 PMJoffrey
03/22/2022, 5:51 PMawaitClose { } block. Check the documentation of callbackFlowSergio C.
03/22/2022, 6:10 PMSergio C.
03/22/2022, 6:10 PMSergio C.
03/22/2022, 6:12 PMephemient
03/22/2022, 6:20 PMephemient
03/22/2022, 6:21 PMSergio C.
03/22/2022, 11:39 PMgildor
03/23/2022, 1:50 AM