Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava doesn't work in Scheduler.io() thread

Tags:

java

rx-java

The problem in that: I have Observable and Subscriber. I try to launch Observable in .io() thread, because it works with files and zip archivers (I won't show the code - is too large), but Observable do nothing!:

Observable<Double> creatingObservable = getCreatingObservable(image);
Subscriber<Double> creatingSubscriber = getCreatingSubscriber();

creatingObservable
        .subscribeOn(Schedulers.io())
        .subscribe(creatingSubscriber);

If I launch code without the subscribeOn() - all work. What is the problem and how to solve it

P.S. System.out.println() doesn't work too. Problem have all Scheduler's threads.

like image 404
bukashka101 Avatar asked Dec 14 '22 05:12

bukashka101


1 Answers

It seems the problem is that the main thread terminated before creatingObservable could emit any values.

The simple solution: make the main thread wait long enough to enable creatingObservable to emit/complete.

Observable<Double> creatingObservable = getCreatingObservable(image);
Subscriber<Double> creatingSubscriber = getCreatingSubscriber();

creatingObservable
        .subscribeOn(Schedulers.io())
        .subscribe(creatingSubscriber);

Thread.sleep(5000); //to wait 5 seconds while creatingObservable is running on IO thread
like image 149
Simon Z. Avatar answered Dec 17 '22 02:12

Simon Z.