Skip to content

Ignore(don't save reference to) disposable returning after subscription. RxJava2 #5503

@aleksei-tvorogov

Description

@aleksei-tvorogov

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions