在室内@Delete注释不会发出任何东西.这就是dao的样子
@Dao
public interface UserDao {
@Delete
void deleteUser(User user);
//We can't use Maybe or Single or anything here
}
这使得在做类似事情时出现问题
userRepository.deleteUser().subscribeOn,因为我们没有排放到dao.我使用以下代码在后台线程上调用deleteUser.
Observable.just(appDatabase).
subscribeOn(SchedulerProvider.getInstance().computation()).
subscribe(db -> {
userRepository.logoutUser(loggedUser.getLoggedInUser());
loggedUser.setLoggedInUser(null);
},this::handleError);
这很好用.但是,在订阅方法中,我现在需要访问Android UI以显示宣布成功删除的Toast.当然,我得到这个例外(因为链中缺少observeOn)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
但是当我把observeOn放在这样的时候
Observable.just(appDatabase).
subscribeOn(SchedulerProvider.getInstance().computation()).
observeOn(SchedulerProvider.getInstance().ui()).
subscribe(db -> {
userRepository.logoutUser(loggedUser.getLoggedInUser());
loggedUser.setLoggedInUser(null);
Message message = new Message(R.string.user_logged_out_msg);
message.setMessageType(Message.MessageType.SUCCESS_MESSAGE);
view.showMessages(Arrays.asList(message)); //this leads to a taost
},this::handleError);
我奇怪地得到这个例外:
cannot access database on the main thread since it may potentially lock the UI for a long period of time.
最佳答案