CalledFromWrongThreadException even when using AndroidSchedulers.mainThread ()

Well, we have implemented an MVP layer to make RxJava and RxAndroid look good and use it. We took this as an example and built it.

When the calling call is launched, it sends Subscriber to the model interactor. Interactor creates an Observable and sets observeOn(Schedulers.io()) and subscribeOn(AndroidSchedulers.mainThread()) . That way (we thought), when the call returns to Subscriber (inside Presenter), each call will be in the UI thread. Inside Subscriber we bind data to the view. However, this raises a CalledFromWrongThreadException :

 07-28 09:12:48.844 17424 17566 AndroidRuntime E FATAL EXCEPTION: RxCachedThreadScheduler-1 07-28 09:12:48.844 17424 17566 AndroidRuntime E Process: [PACKAGE NAME], PID: 17424 07-28 09:12:48.844 17424 17566 AndroidRuntime E java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread. 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.FutureTask.run(FutureTask.java:237) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.lang.Thread.run(Thread.java:818) 07-28 09:12:48.844 17424 17566 AndroidRuntime E Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:201) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:137) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorSubscribeOn$1$1$1.onNext(OperatorSubscribeOn.java:76) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:205) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:159) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 07-28 09:12:48.844 17424 17566 AndroidRuntime E ... 7 more 07-28 09:12:48.844 17424 17566 AndroidRuntime E Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 07-28 09:12:48.844 17424 17566 AndroidRuntime E ... 15 more 07-28 09:12:48.844 17424 17566 AndroidRuntime E Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received => 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.util.Log.getStackTraceString(Log.java:499) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:91) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:66) 07-28 09:12:48.844 17424 17566 AndroidRuntime E ... 7 more 07-28 09:12:48.844 17424 17566 AndroidRuntime E Caused by: java.lang.IllegalStateException: Method call should happen from the main thread. 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.squareup.picasso.Utils.checkMain(Utils.java:136) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.squareup.picasso.RequestCreator.into(RequestCreator.java:615) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.nextmarkets.next.education.view.TradingIdeaHistoryFragment.setCoachAvatar(TradingIdeaHistoryFragment.java:94) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.nextmarkets.next.education.TradingIdeaHistoryPresenter$TradingIdeaHistorySubscriber.onNext(TradingIdeaHistoryPresenter.java:55) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at com.nextmarkets.next.education.TradingIdeaHistoryPresenter$TradingIdeaHistorySubscriber.onNext(TradingIdeaHistoryPresenter.java:38) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorSubscribeOn$1$1$1.onNext(OperatorSubscribeOn.java:76) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:205) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:159) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 07-28 09:12:48.844 17424 17566 AndroidRuntime E ... 7 more 07-28 09:12:48.844 17424 17566 AndroidRuntime E Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7062) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewRootImpl.requestChildFocus(ViewRootImpl.java:3098) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at android.view.ViewGroup.requestChildFocus(ViewGroup.java:678) 07-28 09:12:48.844 17424 17566 AndroidRuntime E at andro 

At this point, the stack is actually disabled.

Of course, we could just run it in the user interface thread manually in the view, but this is not necessary if we set subscribeOn(AndroidSchedulers.mainThread()) , right? We did it without any problems. Are we missing something?

Some implementation details: We use Dagger2 for DI, and Interactor is created in Module and receives Schedulers through the constructor.

  @Provides MyInteractor provideMyInteractor() { return new MyInteractorImpl(Schedulers.io(), AndroidSchedulers.mainThread()); } 

The presenter gets the Interactor through the constructor injection, and the Presenter is introduced into the view through the Component .

+4
source share
1 answer

Not sure what you are doing there, but just to remind and clarify:

The subscribeOn method indicates the thread in which the onSubscribe method will execute.

The observOn method indicates the thread on which onNext / onError / onCompleted will be executed.

You say: "Interactor creates an Observable and sets observOn (Schedulers.io ()) and subscribeOn (AndroidSchedulers.mainThread ())." You may have confused the two methods.

+12
source

All Articles