Skip to content

Instantly share code, notes, and snippets.

@adesamp
Created March 29, 2021 02:57
Show Gist options
  • Save adesamp/824a1a93de43328e8775d8cb7f9fa080 to your computer and use it in GitHub Desktop.
Save adesamp/824a1a93de43328e8775d8cb7f9fa080 to your computer and use it in GitHub Desktop.
fun <T> DataStore<Preferences>.getValueFlow(
key: Preferences.Key<T>,
defaultValue: T,
): Flow<T> {
return this.data
.catch { exception ->
if (exception is IOException) {
emit(emptyPreferences())
} else {
throw exception
}
}.map { preferences ->
preferences[key] ?: defaultValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment