Reputation: 853
Does anyone know operator in rxlava or code example how to return result of observable to thread in which it was called? Actually it is another one cache on Android)) The application is divided on few layers:
UI | Domain (medium) layer(caching, some app logic) | Data layer(disk and network)
The problem is that I don't know from which one thread observable is called from different layers. So I have to do redundant switching to Android main thread or another one Schedullers.computation()
or Schedullers.io()
thread.
For example UI layer subscribes to observable from UI main thread (or any other, for example computation) to domain layer and if cached data is present in returns value immediately. But if no cached value domain layer subscribes to data layer in io thread if data on disk or in computation thread if I need to generate some data and return to thread in which previous layer subscribed.
Of course I have to take in account concurrent data access.
Thanks in advance!
UPD: I just understand than I only have to remove .observeOn(AndroidSchedulers.mainThread())
on any Observable I retun fron Domain layer. I only need to always add final .observeOn(AndroidSchedulers.mainThread())
on subscription or in places where I interact with UI but not on any Domain layer Observable.
Upvotes: 0
Views: 225
Reputation: 70007
I'm not an experienced Android developer, but as far as I know, each thread can have its Looper
associated, along with a Handler
. The RxAndroid library let's you create a Scheduler
from a Handler and you can just observeOn
on the thread associated with the looper.
Upvotes: 0