以下代码片段用于向服务器发送请求并将数据保存到数据库。
sessionRepository.getUserId()// from database
.flatMap(userId -> {
return profileRepository.editProfile(userId, profile);// send request to server
})
.flatMapCompletable(profile1 -> {
return profileRepository.deleteProfile() // from database
.andThen(profileRepository.saveProfile(profile1));// to database
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
getViewState().hideLoadingState();
getViewState().replaceToProperties();
}, throwable -> {
throwable.printStackTrace();
});
问题是它无法访问subscribe(),但是从数据库中删除并成功保存到它。顺序也保留。
但如果我这样做的话,它会起作用:
int userId = sessionRepository.getUserId().blockingFirst();// get user id from database
compositeDisposable.add(
profileRepository.editProfile(userId, profile)// send request to server
.flatMapCompletable(profile1 -> {
return profileRepository.deleteProfile()// from database
.andThen(profileRepository.saveProfile(profile1));// to database
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {
getViewState().replaceToProperties();
getViewState().hideLoadingState();
}, throwable -> {
throwable.printStackTrace();
})
);