Skip to content

Commit

Permalink
fix NPE of livedata when calling clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
beigirad authored and chibatching committed Jan 29, 2022
1 parent 0702f9b commit 855933a
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public fun <T> KotprefModel.asLiveData(property: KProperty0<T>): LiveData<T> {
private val key: String = this@asLiveData.getPrefKey(property)
?: throw IllegalArgumentException("Failed to get preference key, check property ${property.name} is delegated to Kotpref")

override fun onSharedPreferenceChanged(prefs: SharedPreferences, propertyName: String) {
if (propertyName == key) {
override fun onSharedPreferenceChanged(prefs: SharedPreferences, propertyName: String?) {
// propertyName will be null when preferences are cleared on Android R
val isCleared = propertyName == null
if (isCleared || propertyName == key) {
postValue(property.get())
}
}
Expand Down

0 comments on commit 855933a

Please sign in to comment.