-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Subscriber.isUnsubscribed always returns false for synchronous methods #813
Comments
Hi. Generally, |
@akarnokd, no, it never stops. |
As |
@mironov-nsk You are correct in your impression that it should work, that is the purpose of 0.17. However, as @zsxwing said, we don't yet have Right now if you avoid those and use a Scheduler with the synchronous code inside it demonstrates the theory of how it would work while still unsubscribing from the outside: final Subscription subscription = Observable
.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(final Subscriber<? super Integer> subscriber) {
subscriber.add(NewThreadScheduler.getInstance().schedule(new Action1<Inner>() {
@Override
public void call(Inner inner) {
try {
for (int i = 0; !inner.isUnsubscribed(); i++) {
System.out.println("Observable : " + i);
subscriber.onNext(i);
Thread.sleep(2000);
}
} catch (Exception exception) {
subscriber.onError(exception);
}
}
}));
};
})
.subscribe(new Action1<Integer>() {
@Override
public void call(final Integer value) {
System.out.println("Observer : " + value);
}
});
Thread.sleep(3000);
subscription.unsubscribe(); Note how the That said, we definitely want to have your code functional before releasing. |
@benjchristensen, thank you for clarifying that. Provided workaround works in this particular case, but doesn't work in a real case that I have. My Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(final Subscriber<? super String> subscriber) {
try {
final Socket socket = new Socket("127.0.0.1", 12345);
final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
IOUtils.closeQuietly(socket);
}
}));
while (!subscriber.isUnsubscribed()) {
subscriber.onNext(reader.readLine());
}
} catch (Exception exception) {
subscriber.onError(exception);
}
}
}); Server sends a new string very rarely and even if I unsubscribe from the Unfortunately, I couldn't find a way to add an unsubscribe callback using |
You can add multiple Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(final Subscriber<? super String> subscriber) {
try {
final Socket socket = new Socket("127.0.0.1", 12345);
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
IOUtils.closeQuietly(socket);
}
}));
subscriber.add(NewThreadScheduler.getInstance().schedule(
new Action1<Inner>() {
@Override
public void call(Inner inner) {
try {
final BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (!inner.isUnsubscribed()) {
subscriber.onNext(reader.readLine());
}
} catch (Exception exception) {
subscriber.onError(exception);
}
}
}));
} catch (Exception exception) {
subscriber.onError(exception);
}
}
}); |
@benjchristensen, @zsxwing thank you for your help. With your hints I managed to solve my problem, but the code became a bit more complicated than it has been before. Looking forward to the |
Agreed. I hope to get |
This issue seems to be fixed in the |
Excellent, thank you for confirming. |
The following program produces unexpected (at least for me) output:
The output is:
And I'm expecting to get the following output:
Am I wrong or it's a bug?
The text was updated successfully, but these errors were encountered: