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.

Rx without subscribeOn and observeOn

See original GitHub issue

As I see from our project, I barely see subscribeOn and observeOn in api call, how is this possible? even in DetailViewMode, it calls api with .subscribeOn(schedulerProvider.ui()) 😲

but the weird part is everything is working just fine, what am I missing here? @rizmaulana

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rizmaulanacommented, Mar 14, 2020
  1. No need to add subscribeOn on api call because it is already subscribe on io threads when retrofit initialized, here : https://github.com/rizmaulana/kotlin-mvvm-covid19/blob/master/app/src/main/java/id/rizmaulana/covid19/di/NetworkModule.kt
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
  1. Actually we also no need observeOn(schedulerProvider.ui()) because observe method on live data already force to running on main thread, we can see how observe method works here
@MainThread
    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
        assertMainThread("observe");
        if (owner.getLifecycle().getCurrentState() == DESTROYED) {
            // ignore
            return;
        }
        LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
        ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper);
        if (existing != null && !existing.isAttachedTo(owner)) {
            throw new IllegalArgumentException("Cannot add the same observer"
                    + " with different lifecycles");
        }
        if (existing != null) {
            return;
        }
        owner.getLifecycle().addObserver(wrapper);
    }
protected void postValue(T value) {
        boolean postTask;
        synchronized (mDataLock) {
            postTask = mPendingData == NOT_SET;
            mPendingData = value;
        }
        if (!postTask) {
            return;
        }
        ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable);
    }
  1. .subscribeOn(schedulerProvider.ui()) on DetailViewMode : silly me 😅 it must be . observeOn(schedulerProvider.ui()), but fortunately we use live data that makes view running on main thread. So it is not breaking. But why it also not blocked the api call when subscribe on mainThread()? AFAIK, RxJava only accepts first subscribeOn declaration (which is already on io thread) so code .subscribeOn(schedulerProvider.ui()) is ignored, CMIIW
1reaction
isfaaghythcommented, Mar 15, 2020

already test it, you can see the test case for DashboardViewModel, there’s no issue for that

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding RxJava subscribeOn and observeOn
As seen above, subscribeOn() changes the thread on which our Observable is emitted and transformed. In the absence of observeOn() , the results ......
Read more >
Rxandroid What's the difference between SubscribeOn and ...
SubscribeOn specify the Scheduler on which an Observable will operate. ObserveOn specify the Scheduler on which an observer will observe ...
Read more >
RxJava: subscribeOn vs observeOn - Medium
One of the strongest aspects of RxJava is the simple way to schedule work on a desired thread using either subscribeOn or observeOn...
Read more >
Understand subscribeOn and observeOn - effective-rxjava
The subscribeOn method applies to upstream Observable instances. Its Scheduler parameter specifies the thread on which the upstream subscribe method is invoked.
Read more >
SubscribeOn operator - ReactiveX
The ObserveOn operator specifies a different Scheduler that the Observable will use to send notifications to its observers. As shown in this illustration,...
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