Created
March 29, 2021 02:57
-
-
Save adesamp/824a1a93de43328e8775d8cb7f9fa080 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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