Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified of a observer's unsubscribe action in a custom Observable in RxJava

Tags:

I'm trying to wrap some listener-pattern based API to an Observable. My code roughly looks like following.

def myObservable = Observable.create({ aSubscriber ->
    val listener = {event -> 
      aSubscriber.onNext(event);                
    }
    existingEventSource.addListener(listener)
})

However, I want my observable to immediately remove the listener from the underlying existingEventSource when the observer calls subscription.unscribe(). How could I achieve this goal?

like image 262
victorx Avatar asked Nov 02 '14 01:11

victorx


People also ask

How do I unsubscribe from Observable RxJava?

You need to cancel your job properly via Observable::create and Observable::flatMap. And set your cancalable in Observable::create.

Do I need to unsubscribe from a completed Observable?

Yes you are correct. After a stream is terminated ( onComplete / onError has been called ), subscriber unsubscribes automatically. You should be able to test these behaviors using isUnsubscribed() method on the Subscription object.

How do I unsubscribe from RxJS Observable?

Unsubscribing Manually One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS provides us with a convenient method to do this. It lives on the Subscription object and is simply called . unsubscribe() .


1 Answers

The Subscriber abstract class actually has a method add which lets you add Subscriptions which will be unsubscribed with the subscriber.

def myObservable = Observable.create({ aSubscriber ->
    val listener = {event -> 
      aSubscriber.onNext(event);                
    }
    existingEventSource.addListener(listener)

    // Adds a lambda to be executed when the Subscriber un-subscribes from your Observable
    aSubscriber.add(Subscriptions.create(() -> existingEventSource.removeListener(listener)));
})

Think of aSubscriber as the Observer that subscribed to your Observable; we'll call it a Subscriber. As long as the Subscriber is still subscribed to the Observable, the Observable can emit values. But when that Subscriber un-subscribed then it should stop. But if we want to get notified when the Subscriber unsubscribes we can register an Action to be run when it happens. This is what the add method is used for. As mentioned by @dwursteisen in the comments; you're basically registering a lambda that will be executed when the Subscriber un-subscribes.

It's also possible to have the Subscription be unsubscribe on a different Scheduler. See MainThreadSubscription from the rxanroid project for an example of how to achieve that .

Here's an example of how you'd use it for unsubscribing on the main thread

aSubscriber.add(new MainThreadSubscription() {
    @Override
    protected void onUnsubscribe() {
        existingEventSource.removeListener(listener);
    }
});
like image 160
Miguel Avatar answered Sep 21 '22 15:09

Miguel