I'm fairly new to RxJava so this is probably a dumb question. I am going to describe my scenario.
I have some code running on the UI thread which will update some images but those images are not very important and they consume a bit of resources while generating them so I want to generate them on a single thread (not the UI thread of course) and generate them one by one. I'm guessing the trampoline scheduler is what I want but my problem is that if I use it then it does the work on the UI thread and I want it to do it on another thread.
Obviously I can write my own thread in which I can queue items and then it processes those one by one but I thought maybe RxJava would have a simple solution for me?
My current code looks like this:
Observable<Bitmap> getImage = Observable.create(new Observable.OnSubscribe<Bitmap>() { @Override public void call(Subscriber<? super Bitmap> subscriber) { Log.w(TAG,"On ui thread? "+ UIUtils.isRunningOnUIThread()); subscriber.onNext(doComplexTaskToGetImage()); subscriber.onCompleted(); } }); getImage.subscribeOn(Schedulers.trampoline()).subscribe(new Action1<Bitmap>() { @Override public void call(Bitmap bitmap) { codeToSetTheBitmap(bitmap); } });
My log that says "On ui thread?" always has true. So how do I make that code and all subsequent attempts to do the same thing run on a single thread (not the ui thread) in order without writing a bunch of code to queue that work?
Edit:
I believe that this can now be accomplished using Schedulers.single()
or if you want your own you can use new SingleScheduler()
. I'm still testing but I think it does what I wanted back when I posted this.
By default, Rx is single-threaded which implies that an Observable and the chain of operators that we can apply to it will notify its observers on the same thread on which its subscribe() method is called.
observeOn() simply changes the thread of all operators further Downstream. People usually have this misconception that observeOn also acts as upstream, but it doesn't. subscribeOn() only influences the thread which is going to be used when Observable is going to get subscribed to and it will stay on it downstream.
Android Scheduler — This Scheduler is provided by rxAndroid library. This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn method.
RxJava: Multi-Threading in Android.
You can create a single reusable thread to create a Scheduler
for the Observable
in one of the following ways:
ThreadPoolExecuter
with a pool size of 1 (Executors.newSingleThreadExecutor()
is a convenient static factory method for doing that), then use it to generate the schedulers via the Schedulers.from()
method.Scheduler
implementation that uses a Handler
to schedule the actions, and thus can be used with any Thread
that has a Looper
running by passing it's Handler
to the AndroidSchedulers.handlerThread()
factory method.Note that you will need to observe on a main thread Scheduler
if you're interacting with the UI at the conclusion of these tasks.
In RxJava 2 you can use Schedulers.single()
which:
Returns a default, shared, single-thread-backed Scheduler instance for work requiring strongly-sequential execution on the same background thread.
Please see the documentation for more details.
I don't see it available in RxJava 1 Schedulers documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With