rx-java2

delay and interval operators do not work properly

爷,独闯天下 提交于 2019-12-02 08:33:28
As shown below, I am creating Observables . I would like to wait specific amount of time in seconds as shown in the code. therefore I used either delay or interval operator. I expected the code to wait i.e. 5 seconds then System.out.println from the observer to be printed. but what happens is, doOnNext is executed and the code never goes further. I mean the execution stops at doOnNext even after the 5 seconds elapsed. Code : public static void main(String[] args) { Observable<List<Person>> observables = Observable.create(e-> { for(List<Person> p : Main.getPersons()) { e.onNext(p); } e

onNext is not getting called on just operator

时光怂恿深爱的人放手 提交于 2019-12-02 07:54:46
I am using rxjava in eclipse. I cretaed the below posted example. the issue is,, when i run the code, I receive only onSubscribe: 0 and I dont receive any output from onNext. please let me know why I do not receive any output from onNext, and how to fix it code : public static void main(String[] args) { Observable<String> stringObservable = Observable.just("Hello from just Operator1"); stringObservable .subscribeOn(Schedulers.newThread()) .observeOn(Schedulers.io()) .subscribe(new Observer<Object>() { @Override public void onComplete() { // TODO Auto-generated method stub System.out.println(

Mapping exception in RxJava 2

前提是你 提交于 2019-12-02 06:08:00
问题 How can I map one occurred exception to another one in RxJava2? For example: doOnError(throwable -> { if (throwable instanceof FooException) { throw new BarException(); } }) In this case I finally receive CompositeException that consists of FooException and BarException , but I'd like to receive only BarException . Help! 回答1: You can use onErrorResumeNext and return Observable.error() from it: source.onErrorResumeNext(e -> Observable.error(new BarException())) Edit This test passes for me:

How to subscribe several subscribers to Observable or Flowable?

梦想与她 提交于 2019-12-02 05:35:15
In Hello World example there is one subscriber public static void main(String[] args) { Flowable.just("Hello world").subscribe(System.out::println); } How to make two or more? You can subscribe multiple subsctibers to any observable/flowable. Just repeat subscribe call as many times as you need. Flowable<String> source = Flowable.just("Hello world"); source.subscribe(System.out::println); source.subscribe(System.out::println); ... There is difference in hot and cold observables in the way they handle such multiple subscriptions. Cold observables/flowables re-request items from source for every

Full outer join of two ordered observables

心已入冬 提交于 2019-12-02 04:49:08
问题 Suppose we have two observables Observable<Integer> o1 and Observable<Integer> o2 and each observable is producing strictly increasing sequence. The task is to perform equivalent of full outer join on these two observables. For example join of Observable.just(0, 2, 3, 6) Observable.just(1, 2, 3, 4, 5, 6) should produce [ [0, _], [_, 1], [2, 2], [3, 3], [_, 4], [_, 5], [6, 6] ] The join should be efficient and work well on very large or infinite streams. The solution is easy in pull scenario.

Is RxJava a good fit for branching workflows?

主宰稳场 提交于 2019-12-02 03:25:09
I am using RxJava to process some notifications that we pull from a queue. RxJava seemed to work fine with a simple workflow, now with new requirements coming in, the flow is growing in complexity with more branches (please see below picture as a reference) I tried to exemplify the flow with a small unit test: @Test public void test() { Observable.range(1, 100) .groupBy(n -> n % 3) .toMap(GroupedObservable::getKey) .flatMap(m1 -> { Observable<Integer> ones1 = m1.get(0); Observable<Integer> twos1 = m1.get(1).map(n -> n - 10); Observable<Integer> threes = m1.get(2).map(n -> n + 100); Observable

Mapping exception in RxJava 2

守給你的承諾、 提交于 2019-12-02 02:20:56
How can I map one occurred exception to another one in RxJava2? For example: doOnError(throwable -> { if (throwable instanceof FooException) { throw new BarException(); } }) In this case I finally receive CompositeException that consists of FooException and BarException , but I'd like to receive only BarException . Help! You can use onErrorResumeNext and return Observable.error() from it: source.onErrorResumeNext(e -> Observable.error(new BarException())) Edit This test passes for me: @Test public void test() { Observable.error(new IOException()) .onErrorResumeNext((Throwable e) -> Observable

RxJava 1 and RxJava 2 in the same project [duplicate]

被刻印的时光 ゝ 提交于 2019-12-02 01:04:02
This question already has an answer here: How to resolve Duplicate files copied in APK META-INF/rxjava.properties 7 answers Our project uses RxJava 1: compile 'io.reactivex:rxjava:1.1.6' There is a library we use that uses RxJava 2 internally: compile 'io.reactivex.rxjava2:rxjava:2.0.9' When I do ./gradlew assembleDebug I get this error: com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties File1: /Users/darklord/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.1.6

Full outer join of two ordered observables

孤者浪人 提交于 2019-12-02 00:17:40
Suppose we have two observables Observable<Integer> o1 and Observable<Integer> o2 and each observable is producing strictly increasing sequence. The task is to perform equivalent of full outer join on these two observables. For example join of Observable.just(0, 2, 3, 6) Observable.just(1, 2, 3, 4, 5, 6) should produce [ [0, _], [_, 1], [2, 2], [3, 3], [_, 4], [_, 5], [6, 6] ] The join should be efficient and work well on very large or infinite streams. The solution is easy in pull scenario. Is there an idiomatic rx way to achieve this? There is no single operator for this but it is possible

CompositeDisposable.clear causes OkHttp to throw java.lang.IllegalStateException: Unbalanced enter/exit

℡╲_俬逩灬. 提交于 2019-12-01 21:30:33
So I have a simple http request using OkHttp. I do this with RxJava on Android. I add this RxJava call to a CompositeDisposable that I then clear on onStop . For some reason that is triggering this exception below. I'm a little unsure about how to fix it. Caused by java.lang.IllegalStateException: Unbalanced enter/exit at okio.AsyncTimeout.enter(AsyncTimeout.java:73) at okio.AsyncTimeout$2.read(AsyncTimeout.java:235) at okio.RealBufferedSource.read(RealBufferedSource.java:47) at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363) at okhttp3.internal.http1.Http1Codec