简体   繁体   English

RxJava线程切换

[英]RxJava Thread switching

RxBottomNavigationView.itemSelections(sections).map(menuItem -> menuItem.getTitle().toString())
            .observeOn(AndroidSchedulers.mainThread()).map(this::insertCategoryHeadersForSection)
            .subscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread())
            .compose(bindToLifecycle()).subscribe(itemInfos -> dishRecyclerAdapter.animateTo(itemInfos));

The code above throws this exception: 上面的代码抛出此异常:

java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean io.reactivex.internal.fuseable.SimpleQueue.isEmpty()' on a null object reference
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method)

I am trying to get the itemSelections event from a BottomNavigationView, then map the item to its title (on the main thread), then run a long operation on a background thread, then receive the output of that operation on the main thread again. 我正在尝试从BottomNavigationView获取itemSelections事件,然后将该项目映射到其标题(在主线程上),然后在后台线程上运行长操作,然后再次在主线程上接收该操作的输出。 Help please. 请帮助。 thanks in advance. 提前致谢。

You've run into a bug in RxBinding2 that is fixed in the repo but not yet released. 在RxBinding2中遇到了一个错误,错误已在存储库中修复,但尚未发布。 The reason for it is that by using subscribeOn(Schedulers.computation()) you don't move the computation into the background but make the RxBottomNavigationView.itemSelections(sections) happen on the background thread which is forbidden and crashes your stream (and app). 其原因是,通过使用RxBottomNavigationView.itemSelections(sections) subscribeOn(Schedulers.computation())您不会将计算移入后台,而是使RxBottomNavigationView.itemSelections(sections)发生在后台线程上,这会导致该线程(和应用程序)崩溃)。

You should do this instead: 您应该这样做:

RxBottomNavigationView.itemSelections(sections)
.subscribeOn(AndroidSchedulers.mainThread())      // <------------------------
.map(menuItem -> menuItem.getTitle().toString())  // main thread
.observeOn(Schedulers.computation())              // <------------------------
.map(this::insertCategoryHeadersForSection)       // background
.observeOn(AndroidSchedulers.mainThread())        // <------------------------
.compose(bindToLifecycle())
.subscribe(itemInfos ->                           // main
    dishRecyclerAdapter.animateTo(itemInfos));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM