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

[0.16.x] Unable to subscribe several times to a GroupedObservable produced by groupBy #957

Closed
amazari opened this issue Mar 12, 2014 · 2 comments

Comments

@amazari
Copy link
Contributor

amazari commented Mar 12, 2014

It seems impossible to subscribe several Observers to the same GroupedObservable, as demonstrated in the following snipet:

Observable.range(1, 100).groupBy(new Func1<Integer, String>() {
  public String call(final Integer integer) {
    return integer > 50 ? "big" : "small";
  }
}).subscribe(new Action1<GroupedObservable<String, Integer>>() {
  public void call(final GroupedObservable<String, Integer> objectIntegerGroupedObservable) {

    // First subscription, if left alone works flawlessly
    objectIntegerGroupedObservable.subscribe(new Action1<Integer>() {
      public void call(final Integer integer) {
        System.out.println("first subscriber: "+integer);
      }
    });

    // Second subscription, fails and break the first one too
    objectIntegerGroupedObservable.subscribe(new Observer<Integer>() {
      public void onCompleted() {}
      public void onError(final Throwable e) { System.out.println(e); }
      public void onNext(final Integer args) { }
    });
  }
});

When providing an onError implementation to the second subscription, we can see a "java.lang.IllegalStateException: Can not set subscription more than once."
This seems unique to GroupedObservable, as other Observables (direct, or resulting of transformation) can be subscribed to multiples times.
Is this a bug or a desired behavior ? In the latter case, I am interested in the rationals and alternatives.
I am currently using publish and/or replay on the resulting GroupedObservables before any further processing but it clutters the code with implemetation detailsm imho.

Thanks for your time and energy !

@akarnokd
Copy link
Member

Hi. Yes, it is the common single-subscription bug. If you can, upgrade to the most recent version (0.17).

A few of the older operators have this issue. For a workaround, I suggest using groupByUntil() with a Observable.never() duration selector. However, this operator has a synchronization bug, therefore, don't use it with any other kind of duration selector.

@amazari
Copy link
Contributor Author

amazari commented Mar 12, 2014

Thanks for your answer. I am gonna look into the alternatives you suggest.

@amazari amazari closed this as completed Mar 12, 2014
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

No branches or pull requests

2 participants