更新Room数据库中的实体

时间:2017-11-23 20:55:35

标签: android rx-java rx-java2 android-room

我在更新Room数据库中的单个实体方面遇到了问题。我已经设置了一个实体User,它保存了与用户相关的所有数据,例如id,username等。

我也在使用ViewModel模式,以及使用RxJava构建的Room。数据库在MainActivity中创建,其中还包含userViewModel

factory = Injection.provideViewModelFactory(this);
userViewModel = ViewModelProviders.of(this,factory).get(UserViewModel.class);

然后,我有一组任意操作来检查用户数据是否得到更新。我创建User对象,设置属性,将User对象插入数据库,获取set属性以查看它们是否被返回,重新设置它们并再次获取它们以观察更改(免责声明) :这段代码很难看,仅用于帮助/调试目的):

    User user = new User();
    user.setId("id");
    user.setUsername("username");
    Observable.just(userViewModel.insertUser(user))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> Log.println(Log.ASSERT, "Updating ...", "Updated User"));
    CompositeDisposable disposable = new CompositeDisposable();
    Observable.just(userViewModel.getUserId()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> Log.println(Log.ASSERT, "ID", s))
    );
    disposable.add(userViewModel.getUserName()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> Log.println(Log.ASSERT, "Username", s))
    );
    disposable.add(userViewModel.updateUserName("new username")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(() -> {
                Log.println(Log.ASSERT, "Completable", " called");
            })
    );
    disposable.add(userViewModel.updateId("20")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe()
    );
    Observable.just(userViewModel.getUserId()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> Log.println(Log.ASSERT, "NEW ID", s))
    );
    Observable.just(userViewModel.getUserName()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> Log.println(Log.ASSERT, "NEW Username", s))
    );

更新方法如下:

public Completable updateUserName(final String userName){
    Log.println(Log.ASSERT, "Updating...", userName);
    return new CompletableFromAction(() -> {
        mUser = mUser == null
                ? new User(userName)
                : new User(mUser.getId(),userName);
        Log.println(Log.ASSERT, "New user", mUser.getUsername());
        mDataSource.updateUser(mUser);
    });
}

public Completable updateId(final String id){
    Log.println(Log.ASSERT, "Updating....", id);
    return new CompletableFromAction( () -> {
        mUser = mUser == null
                ? new User()
                : new User(mUser.getId());
        Log.println(Log.ASSERT, "New id", mUser.getId());
        mUser.setId(id);
        mDataSource.updateUser(mUser);
    });
}

我正在使用的UserDao方法是:

@Update
fun updateUser(vararg user: User)

User对象的构造函数:

constructor(){

}

@Ignore
constructor(userName: String) {
    this.username = username
}

constructor(id: String, userName: String) {
    this.id = id
    this.username = userName
}

在调试和查看日志语句时,这就是我得到的:

11-23 21:44:25.131 15091-15091/com.myapp.app A/Updating...: new username
11-23 21:44:25.134 15091-15091/com.myapp.app A/Updating....: 20
11-23 21:44:25.835 15091-15091/com.myapp.app A/Updating ...: Updated User
11-23 21:44:25.836 15091-15091/com.myapp.app A/Completable:  called
11-23 21:44:25.836 15091-15091/com.myapp.app A/Username: username
11-23 21:44:25.836 15091-15091/com.myapp.app A/ID: id
11-23 21:44:25.836 15091-15091/com.myapp.app A/NEW ID: id
11-23 21:44:25.836 15091-15091/com.myapp.app A/NEW Username: username

我做错了什么?未显示update(...)方法中包含的日志,这意味着CompletableFromAction可能存在问题?或者我做错了什么?

0 个答案:

没有答案