question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RxJava2.0 - We may need a `Observable.createDisposable(...)`

See original GitHub issue

I run into problem when creating Observable with: Observable.create(new ObservableOnSubscribe()) When new ObservableOnSubscribe implements Disposable it is expected to call dispose() method to release some resources, that is not happening.

Observable created with Observable.create(..) doesn’t implement Disposable and the upstream clean up isn’t passing through to ObservableOnSubscribe.

I am open to discussion and suggest small change with: Observable.java add method:

public static <T> Observable<T> createDisposable(DisposableObservableOnSubscribe<T> observableOnSubscribe) {
        return Observable.create(observableOnSubscribe).doOnDispose(observableOnSubscribe::dispose);
    }

with adding new Interface of DisposableObservableOnSubscribe:

public interface DisposableObservableOnSubscribe<T> extends ObservableOnSubscribe<T>, Disposable {

}

Or doing it other way - like I did on: gist

I didn’t found something that would solve this use case.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
JakeWhartoncommented, Dec 23, 2016

Here’s a simpler implementation:

public final class RxBroadcastReceiver implements ObservableOnSubscribe<Intent> {
    public static Observable<Intent> create(Context context, IntentFilter intentFilter) {
        return Observable.create(new RxBroadcastReceiver(context, intentFilter));
    }

    private final Context context;
    private final IntentFilter intentFilter;

    private RxBroadcastReceiver(Context context, IntentFilter intentFilter) {
        this.context = context;
        this.intentFilter = intentFilter;
    }

    @Override
    public void subscribe(ObservableEmitter<Intent> emitter) throws Exception {
        final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                emitter.onNext(intent);
            }
        }
        context.registerReceiver(broadcastReceiver, intentFilter);
        emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
            @Override
            public void run() {
                context.unregisterReceiver(broadcastReceiver);
            }
        }));
    }
}
0reactions
magilluscommented, Dec 27, 2016

It worked like a charm, thank you Jake.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava2: Dispose an Observable chain | by Sachin Chandil
If you are learning RxJava2 or already an experienced developer, you must be ... If there were four Observables, we would need to...
Read more >
Modern Android development with Kotlin Part 3 - ProAndroidDev
You can start with RxJava2. Still, it is good for you as an Android developer to know both cause you can be involved...
Read more >
When and how to use RxJava Disposable - Cups of Code
They 're meant to give power to the Observers to control Observables . ... While Observable represents a data flow where data can...
Read more >
Subscribe Conditionally to rxJava2 Observable - Stack Overflow
For now there is no such method is available in RxJava2. But if you are using kotlin you can do it using extension...
Read more >
Observable and Observer - Reactive Programming with RxJava
onNext() can be invoked zero, one, or multiple times by the Observable whereas ... Looking at the Observable and Observer definitions, we can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found