Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when targeting devices running OS versions Android R or later #141

Open
SiimKinks opened this issue Sep 1, 2020 · 6 comments

Comments

@SiimKinks
Copy link

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)

@davelogan
Copy link

davelogan commented Sep 22, 2020

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)

@f2prateek
Copy link
Owner

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.

@mseroczynski
Copy link

@f2prateek are you still working on this?

@f2prateek
Copy link
Owner

I haven't worked on this - feel free to send a PR!

@banasiak
Copy link
Contributor

banasiak commented Feb 9, 2021

@f2prateek @mseroczynski I'll try to get a PR for this with the recommend changes up sometime this week.

banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 9, 2021
banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 9, 2021
banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 9, 2021
banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 10, 2021
banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 10, 2021
@banasiak
Copy link
Contributor

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.

banasiak added a commit to banasiak/rx-preferences that referenced this issue Feb 12, 2021
f2prateek added a commit that referenced this issue Feb 12, 2021
Issue #141: Emit defaultValue if underlying onSharedPreferenceChanged…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants