-
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
Synchronous adapter methods #172
Comments
Many of these have recently been implemented or are being implemented.
You can see the recent releases at https://github.com/Netflix/RxJava/blob/master/CHANGES.md |
Silly me for expecting these to be documented in the wiki. I don't like the way that toIterable() and friends wrap use (And I think that |
No one has gotten around to updating the wiki with the docs ... these functions were just recently submitted as contributions. The try/catch block is what is used on any of the blocking operators (forEach, toIterable, etc). For example, see this unit test: https://github.com/Netflix/RxJava/blob/master/language-adaptors/rxjava-groovy/src/test/groovy/rx/lang/groovy/ObservableTests.groovy#L227 What don't you like about how toIterable throws exceptions? It is a blocking structure so will thrown an exception when iterable.next() is invoked and It can't throw an exception only on the toIterable() method itself since the data is coming asynchronously behind the scenes and we may not know about an error until n elements into the iteration at which point an exception is thrown. As for (Note that many of these design decisions are modeled after Rx.Net which has gone through a long design cycle that we're leveraging for many of these API designs, and they do Also, there is nothing that guarantees only a single Exception type will be thrown from even a single Observable sequence - for example, any variety of RuntimeExceptions can still be passed via onError, not just the strongly typed E if that was declared. Error handlers such as A better way of doing that will exist once the Catch operator (#28) is implemented which will allow defining a |
What I don't like about how toIterable wraps exceptions is that it wraps them in a plain RuntimeException, so the caller cannot in the condition of a catch block distinguish exceptions returned by the Observable from exceptions thrown by the RxJava framework itself. Furthermore, one cannot distinguish in the condition of a catch block amongst different types of exceptions returned by the Observable. |
What are you suggesting as the alternative? The java.util.Iterator interface doesn't throw checked Exceptions, so if onError receives an Exception instead of RuntimeException we still have to throw it somehow since we're now in a synchronous Iterator code flow. Thus, if we receive a RuntimeException, we throw it as is, if we receive an Exception (which I would argue just shouldn't be used as checked exceptions have exactly this problem) we have to wrap it in a RuntimeException so we can throw it. public static RuntimeException propagate(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new RuntimeException(t);
}
} I do not know how to handle the checked exceptions differently but if you can offer an alternative please provide a diff or pull request that demonstrates how you would implement the following code such that it can receive either Exception or RuntimeException and conform to the Iterable/Iterator interfaces:
Here is an example unit test that receives an Exception when calling @Test(expected = TestException.class)
public void testToIteratorWithException() {
Observable<String> obs = Observable.create(new Func1<Observer<String>, Subscription>() {
@Override
public Subscription call(Observer<String> observer) {
observer.onNext("one");
observer.onError(new TestException());
return Subscriptions.empty();
}
});
Iterator<String> it = toIterator(obs);
assertEquals(true, it.hasNext());
assertEquals("one", it.next());
assertEquals(true, it.hasNext());
it.next();
} This code is from: https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/operators/OperatorToIterator.java#L113 |
Per pull #185, my proposed wrapper type is a subclass of |
Closing this out as per the long discussion in #185 |
It would be useful for
Observable<T>
to provideList<T> get() throws Exception
andT getSingle() throws Exception
methods for providing synchronous interfaces on top of Observable.The text was updated successfully, but these errors were encountered: