公司动态

为什么LiveData的数据更新操作页面可以在子线程

📅 2026/7/23 7:27:08
为什么LiveData的数据更新操作页面可以在子线程
现象下面这个代码子线程调用onNvaigate更新页面是可以正常运行的Disposable disposable RxHttp.get(url) .toObservable(HttpRespone.class) //.observeOn(AndroidSchedulers.mainThread()) .subscribe(s - { if (s.getCode()200){ if (callback!null)callback.onNavigate(new NavigationEvent(NavigationEvent.RETURNED,1)); }else { Log.e(TAG,上报错误: s.getMsg()); } },throwable - { Log.e(TAG,http上报异常: throwable); });原因LiveData的数据更新会强制切换到主线程所以运行正常protected void postValue(T value) { boolean postTask; synchronized (mDataLock) { postTask mPendingData NOT_SET; mPendingData value; } if (!postTask) { return; } ArchTaskExecutor.getInstance().postToMainThread(mPostValueRunnable); }