Reputation: 139
I am working on application that utilizes RxJava, realm and retrofit.
I need to create very specific data processing chain. I need to perform Retrofit calls on the io
scheduler, then process provided data on my custom single-thread realm
scheduler, and then push results out to my ui on mainThread
scheduler. I tried to do this by using multiple combinations of observeOn
and subscribeOn
but I can't get the middle part to execute on realm
sheduler.
my goal is something like this
scheduler: io ---------------> realm -----------------> mainthread
action : retrofit call-----> database update -------> ui update
How can I create such chain of observables, where each observable work is done on specific thread ?
Thank you
Upvotes: 0
Views: 491
Reputation: 25573
Look into the Observable.observeOn(...)
method. This method will make sure all operations that happen after occur on the provided Scheduler.
To support your custom "realm" scheduler you will need to provide some way for RxJava to schedule work. If you're using a standard Executor
(like Executors.newSingleThreadExecutor()
to support Realm's single-thread policy) then you can use Schedulers.from(Executor)
to create a scheduler that will execute on that specific Executor
.
ExecutorService realm = Executors.newSingleThreadExecutor();
Scheduler realmScheduler = Schedulers.from(realm);
Retrofit source;
source.call()
.subscribeOn(Schedulers.io())
.observeOn(realmScheduler)
.map(DataBaseUpdate)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(UIUpdate)
Upvotes: 4