Is there any way to say "switch back to caller thr...
# rx
g
Is there any way to say "switch back to caller thread"? For example, I want to wrap some callback call into a single, but the implementation of the callback is switching my thread to
Main
. As a result, the whole chain is now on the
Main
, which is not obvious, if you look at the declaration. Ex:
Copy code
....
   .map { single_that_switches_the_thread_to_Main() }
   .map {}  // <--- NOT io
  .subscribeOn(io)
So, I'd like to be able to tell to this
single_that_switches_the_thread_to_Main()
to switch to the caller thread, inside of it, not on the usage side.
g
is the caller thread the "io" thread since you have the
subscribeOn(io)
?
g
in this particular case yes, the caller thread is IO. The thing is, you can't do assumptions about caller thread when you create the Single inside
single_that_switches_the_thread_to_Main()
g
so calling
observeOn(io)
after the
single_...
wouldn't do it because you are not sure the caller thread was io?
g
observeOn(io)
does the trick of course. But it's a fix on the usage side. Let say I'm exposing this
single_that_switches_thread
. Now, I need to make sure to tell everybody "hey, warning, it fuck ups your chain, fix it with observeOn!". And then you have this hacks all over the code. I was wondering if it's possible to fix it on the creation side, in the provider of this single itself.
r
You could have
single_that_switches_thread
exposed through a method that takes a “source thread” scheduler and then just call
observeOn
for the caller
that way no one can have access to
single_that_switches_thread
without explicitly stating which thread it’s being called from
g
yes.. that would be the last resort. Was just curious if it's possible to make it to operate on a "non particular thread", like most of the rx operators do. Thank you!
r
I’m not sure if I’m misunderstanding you, but there is a
newThread
scheduler.
u
no, if it applies its scheduler then you cannot undo it, just switch somewhere else once it emits
👍 1