Rxjava: Question about takeUntil and Subscription.unsubscribe()

Created on 14 Jul 2016  Â·  9Comments  Â·  Source: ReactiveX/RxJava

in android ,if I use takeUntil for any network and on activity destroy, I use PublishSubject.onNext(null), the relation is gone; if I use CompositeSubscription.add() for any network and on activity destroy, I use CompositeSubscription.unsubscribe(), the relation is gone. what is different ?

Question

Most helpful comment

The detach is shared between all of them so if you signal with it, all the other consumers and any future consumers will be completed.

All 9 comments

I'm sorry, I can't understand what you are asking. Could you post some example code?

You will have better luck with this type of questions on stackoverflow, the editing system there allows other people to help out with the actual question.

Just use the rxjava tag.

@akarnokd
I'm sorry about my english, and I give some code in android.

PublishSubject<Void> detachSignal = PublishSubject.create();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Observable<Object> observable = Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                System.out.print("this is a network");
            }
        });

        observable.takeUntil(detachSignal)
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        detachSignal.onNext(null);
    }
private CompositeSubscription mSubscription;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSubscription.add(Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                System.out.print("this is a network");
            }
        }).subscribe(new Action1<Object>() {
            @Override
            public void call(Object o) {

            }
        }));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSubscription.unsubscribe();
    }

what is different for destroy the relation between Observer and the Observered

@alexandru-calinoiu
thank you for you notice

They should be functionally identical if there is only a single consumer.

@akarnokd thank you for your answer and if there are several consumer , have any different?

The detach is shared between all of them so if you signal with it, all the other consumers and any future consumers will be completed.

@akarnokd thank you so much, I got it.

I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.

Was this page helpful?
0 / 5 - 0 ratings