-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add fromSharedPreferencesChanges Observable #25
Conversation
this useful to listen for SharedPreferences changes
I usually use Observable<T> fromSharedPreferencesChanges(SharedPreferences preferences, String key, Function2<SharedPreferences, String, T> extractor);
Observable<Integer> observable = fromSharedPreferencesChanges(preferences, "key_to_observer", new Function2<SharedPreferences, String, Integer> () {
@Override
public Integer call(SharedPreferences preferences, String key) {
return preferences.getInt(key, 0);
}
}; Anyway, LGTM. |
@mironov-nsk I was thinking about it too. Actually I also use modified version of this code that listen only for particular key. |
@Override | ||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||
if(!subscriber.isUnsubscribed()) | ||
subscriber.onNext(key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a style remark: the indentation is off, and we should always use curly braces even for one liners. It makes it more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, is this check necessary? RxJava makes a best effort at unsubscribing, e.g. when the sequence completes or fails. In general it shouldn't be necessary to check a subscription before emitting a notification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About subscriber.isUnsubscribed()
check:
I was just following instructions from https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators#other-considerations.
Sometimes I'm not sure should I check for it or not.
👍 except for the remark above Let me know if or how you want to address it, then I'll land it |
And one more point I want to discuss: |
Good questions. I think it's something we have to find out with time. That's exactly why I want the project to remain in pre-release status for a while, since I anticipate there to be breaking API changes still I'll merge this, but maybe you can open a question issue where we can gather feedback around this, and if necessary, rename things in one fell swoop. |
Add fromSharedPreferencesChanges Observable
This is wrapper around SharedPreferences.OnSharedPreferenceChangeListener
It's pretty simple and straightforward, but is useful to observe SharedPreferences changes.