question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to get a Scheduler from OnSubscribe function?

See original GitHub issue

Hello.

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:closed
  • Created 10 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
Ghedeoncommented, Jul 5, 2017

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:

private static class MyOnSubscribe implements ObservableOnSubscribe<Foo> {
    @Override
    public void subscribe(ObservableEmitter<Foo> emitter) throws Exception {
        // worker thread
        callbackManager.registerCallback( //some sdk method that always delivers on main
                new Callback() {
                    @Override
                    public void onSuccess(@NonNull Foo result) {
                        // Main Thread. How to move onSuccess() back to worker?
                        emitter.onSuccess(result);
                    }
                }
         ...
        );
    }
}

Yes, I can just leave it as it is, let it deliver on main and fix it later with observeOn. But it just feels right to do it here, rather than outside.

EDIT: onSubscribe --> observeOn

0reactions
lwasylcommented, Oct 8, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found