Open SiimKinks opened 4 years ago
This is causing issues for my app as well after updating target version to 30.
Caused by: java.lang.NullPointerException: onNext called with null. Null values are generally not allowed in 2.x operators and sources. at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:62) at com.f2prateek.rx.preferences2.RxSharedPreferences$1$1.onSharedPreferenceChanged(RxSharedPreferences.java:44) at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:629) at android.app.SharedPreferencesImpl$EditorImpl.lambda$notifyListeners$0$SharedPreferencesImpl$EditorImpl(SharedPreferencesImpl.java:643)
The easy fix here would be to add a null
check here. However this would result in no notification being fired when preferences are cleared, which would be less than ideal.
I think we could also make it so that it emits the default value when this happens.
@f2prateek are you still working on this?
I haven't worked on this - feel free to send a PR!
@f2prateek @mseroczynski I'll try to get a PR for this with the recommend changes up sometime this week.
I wasn't prepared to update Robolectric to support API 30, so the supplied unit test doesn't run. But, otherwise the PR in #143 fixes this in a way suggested by @f2prateek.
Looks like there has been behavior change in OnSharedPreferenceChangeListener onSharedPreferenceChanged callback when apps target devices running OS versions Android R or later. Now when preferences are cleared
null
key will be received in onSharedPreferenceChanged which results in app crashing due to null value being emitted to rx stream.Please see: https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences,%20java.lang.String)