Is there any way to get a Scheduler from OnSubscribe function?
See original GitHub issueHello.
My Observable
is very simple and looks like:
Observable.create(new Observable.OnSubscribe<Content>() {
@Override
public void call(final Subscriber<? super Content> subscriber) {
final ContentObserver observer = new ContentObserver() {
@Override
public void onContentChanged() {
subscriber.onNext(loadContent());
}
};
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
unregisterContentObserver(observer);
}
}));
registerContentObserver(observer);
subscriber.onNext(loadContent());
}
});
loadContent
is a heavy operation and have to be executed on the background thread (which is provided by the Scheduler
that I pass to subscribeOn
method).
PROBLEM
onContentChanged
is always invoked from the UI thread and this cannot be changed, so I need to reschedule loadContent
call to the background thread, but I failed to find a way to get a current Scheduler
.
What is the right way to do it?
Issue Analytics
- State:
- Created 10 years ago
- Comments:16 (11 by maintainers)
Top Results From Across the Web
Schedulers in RxJava - Baeldung
We can obtain a Scheduler from the factory methods described in the class ... Calling the unsubscribe method on a worker will result...
Read more >Scheduler - RxJS
A Scheduler is a data structure. It knows how to store and queue tasks based on priority or other criteria. A Scheduler is...
Read more >RxJava: How to unit test that an Observable ... - Stack Overflow
The way I did it was writing two unit tests as follows: @RunWith(MockitoJUnitRunner.class) class GetItemsTest { private GetItems getItems; ...
Read more >Overview - ReactiveX
A subscribe call is simply a way to start an "Observable execution" and deliver values or events to an Observer of that execution....
Read more >So how does RxJS QueueScheduler actually work?
As you can see we have null and queueScheduler both emitting data in the same ... But since no scheduler is fed to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think @benjchristensen nailed it. I believe
Schedulers.current()
could be very useful for wrapping some non-rx code that is switching threads for some reason. Happens on Android often, when callback is called on main thread. Consider this example:Yes, I can just leave it as it is, let it deliver on
main
and fix it later withobserveOn
. But it just feels right to do it here, rather than outside.EDIT: onSubscribe --> observeOn
Sorry for digging up the thread, but we’re facing the same issue as OP – callback is called on a different thread than the subscription, and we’d like to emit value on the original
subscribeOn
scheduler anyway. Did anything change with RxJava2? I don’t believe https://github.com/ReactiveX/RxJava/issues/941#issuecomment-125480042 is sufficient if we really care about going back to the same scheduler without having to inject it manually. I can also open a new issue if that’s more appropriate