Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore furthur messages after entering terminate state #1657

Merged
merged 1 commit into from
Sep 5, 2014

Conversation

zsxwing
Copy link
Member

@zsxwing zsxwing commented Sep 2, 2014

As per #1633

@girishkolantra
Copy link

I have a question about implementing custom Subscribers. Should we always check for isUnsubscribed on the subscriber before doing further processing inside onNext onError and onCompleted? What's the best practice when implementing a custom Subscriber?

@zsxwing
Copy link
Member Author

zsxwing commented Sep 2, 2014

I have a question about implementing custom Subscribers. Should we always check for isUnsubscribed on the subscriber before doing further processing inside onNext onError and onCompleted? What's the best practice when implementing a custom Subscriber?

You don't need to check it. If you use subscribe(...), rxjava will wrap your subscriber with a SafeSubscriber which will check isUnsubscribed for you.

Usually, subscribe is enough. Some RxJava operators will use unsafeUnsubscribe(...) to avoid too many SafeSubscriber wrappers and improve the performance. But it's used only for implementing an Operator that requires nested subscriptions.

If you are not implementing an Operator, you should use subscribe.

@benjchristensen
Copy link
Member

SafeSubscriber actually doesn't check isUnsubscribed. It filters out onNext after a terminal event (onError or onCompleted).

There are very few places where unsubscribe is triggered, such as take and that operator filters out anything received after unsubscribe because that is the job of that operator. If a Subscriber is invoking unsubscribe and is sensitive to race conditions it can check for isUnsubscribed but rarely is a user provided Subscriber doing this (generally something like take is doing it).

@girishkolantra What do you mean by "custom Subscriber"?

@girishkolantra
Copy link

I meant any subscriber that we implement. I have seen some sample code with checks isUnsubscribed and others that dont. I just wanted to understand what the differences are and when to use what. Thanks for taking the time to answer my questions.

@cloudbees-pull-request-builder

@zsxwing
Copy link
Member Author

zsxwing commented Sep 3, 2014

@girishkolantra we can continue the discussion here: #1633

@zsxwing
Copy link
Member Author

zsxwing commented Sep 3, 2014

@benjchristensen do I need to send another PR for 1.x?

benjchristensen added a commit that referenced this pull request Sep 5, 2014
Ignore furthur messages after entering terminate state
@benjchristensen benjchristensen merged commit 4dc2e7a into ReactiveX:0.20.x Sep 5, 2014
@benjchristensen
Copy link
Member

@zsxwing I think what we're really missing is unsubscribe like onErrorResumeNext has.

That said, it won't help to unsubscribe when inside a parallel since it doesn't really make sense to unsubscribe on an inner parallel Observable.

We may also want to add these same terminal state checks to onErrorResumeNext since a source that doesn't obey unsubscribe could still emit.

@mttkay
Copy link
Contributor

mttkay commented Sep 7, 2014

@benjchristensen Why not add the check for isUnsubscribed in SafeSubscriber? I believe that might also fix this issue: #1590

@zsxwing zsxwing deleted the fix-onErrorReturn branch October 3, 2014 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants