-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Description
Hello, and thx for your great work. I'm Android developer and I faced with some situation. I use Observable.create(...) to wrap listeners and move into reactive-world:
Observable.create(emitter -> {
SharedPreferences sharedPreferences = getSharedPreferences();
SharedPreferences.OnSharedPreferenceChangeListener listener = (sharedPreferences, key) -> emitter.onNext(sharedPreferences.getBoolean("Some key", false));
sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
emitter.setCancellable(() -> sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener));
});
The problem is, this listener is stroring as WeakReference at SharedPreferences.
In that case, only Disposable, returning after Observable.subscribe(...) method will save strong-reference to this listener.
Sometimes it does not neccessary for me to persist that Disposable, because i won't dispose() my subscription. But in this case( don't store reference to Disposable), my listener will be cleared by GC and my Observable stops emit items.
My question: Whether it is valid to not store Disposable, returning after subscribe(...) or not in common case.
Or my code is wrong and I need to create some proxy-class, that will store strong-references in this case
Thx!