从未使用过的观察者方法
作者:互联网
我仍在尝试找出RxJava中的观察者.我有一个Retrofit客户端,一个实用程序方法和一个管理数据同步功能的类.当我尝试观察我的观测值时,我没有看到观察者已订阅的任何迹象.
我的效用方法
public static Single<Response<SyncResponse>> getSyncData() {
Single response = FestApiClient.getInstance().postSyncData()
.observeOn(Schedulers.newThread())
.subscribeOn(AndroidSchedulers.mainThread());
return response;
}
我的改装客户
public Single<Response<SyncResponse>> postSyncData() {
Single response = mFestApiClientService.postEventSyncList("my endpoint");
return response;
}
还有我的数据同步管理器
Disposable syncDisposable = ScheduleUtils.getSyncData().subscribe(syncResponse -> {
if (syncResponse.isSuccessful()){
Log.d(TAG, "Successfully posted events!");
addEventsFromSync(syncResponse.body());
mSyncDialog.dismiss();
} else {
getFailureMessage(syncResponse.body());
Log.d(TAG, "Failed posting events");
}
}, throwable -> {
Log.d(TAG, "Failed posting events");
});
mCompositeDisposable.add(syncDisposable);
我以为syncResponse.onFailure和onSuccess方法会被命中,但是我从没看到日志消息或命中断点.如果您让我知道是否发现有异味,我将不胜感激.谢谢!
解决方法:
您正在观察错误的线程.切换AndroidSchedulers,使其在主线程上.
public static Single<Response<SyncResponse>> getSyncData() {
Single response = FestApiClient.getInstance().postSyncData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
return response;
}
标签:rx-java2,retrofit2,android 来源: https://codeday.me/bug/20191109/2010946.html