I have created my DataStore and I have a boolean value in it:
private val DID_REBOOT = booleanPreferencesKey("did_reboot")
val didRebootFlow: Flow<Boolean> = context.dataStore.data
.map { preferences ->
preferences[DID_REBOOT] ?: false
}
suspend fun setDidReboot(didReboot: Boolean) {
context.dataStore.edit { preferences ->
preferences[DID_REBOOT] = didReboot
}
}
Now, on my BroadcastReceiver I need to check this value. Depending on the the value, I need to perform database operations:
GlobalScope.launch {
if(ds.didRebootFlow.first()) {
Log.d("-----------------", "true")
} else
Log.d("-----------------", "false")
}
In some place in my app, I am setting didRebootFlow to true. But my broadcast is printing false (in the second attempt it works fine).
2021-04-27 12:42:24.347 7469-7469/com.test.datastore D/-----------------: false
I am new to suspended functions and Flow and cannot figure it out how to make my code work.
My code logic is strongly dependant on the value of this field. How to get didRebootFlow once (and immediately) here?
I do not want to use .collect { } because I do not want "observe" didRebootFlow continuously. I would like to get its value only once. (Because observing didRebootFlow non-stop means I will be doing lots of database operations).