RxJava,Room:使用 rxjava 链调用更新 Db 的所有行的最佳方式
RxJava, Room: Best way to update all the rows of Db using rxjava chain calls
我正在使用 rxjava & Room 尝试更新数据库中的行列表。
卡在事件不断触发的循环中
道
@Query("SELECT * FROM movies")
Flowable<List<Movie>> getMovies();
@Update
int updateMovie(Movie movie);
UpdateClass - 助手 classes 正在像这样更新 Db
// Trying to get all existing movies and update one value in all of them.
@WorkerThread
Flowable<Integer> updateMovies(Helper help) {
return movieDao.getMovies().flatMapIterable(movies -> movies)
.flatMap(movie -> {
LogUtils.debug("movieusecase", "movieid" + movie.getMovieId());
movie.updateRating(help.getUpdatedVal(movie));
return Flowable.just(movie);
}).map(movie -> {
return movieDao.updateMovie(movie);
});
}
一旦包含 updateMovie
调用,我就会陷入无限循环,重复事件不断来自房间 db。
Presenter Class - 调用更新 class 来更新数据库中的内容。在 activity onCreate
中触发
updateClass.updateMovies()
.observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(movie -> {
// update stuff
}, throwable -> {
LogUtils.error(TAG, throwable.getMessage());
});
在此先感谢任何帮助。
听起来像是 Single
而不是 Flowable
的工作。
不同之处在于 Single
只会对数据库中的当前电影列表触发一次,它不会像 Flowable
那样不断发出变化。
Single<List<Movie>> getMovies();
或者如果您出于某种原因希望您的方法 return Flowable
,您也可以使用 Flowable.firstOrError()
将您的 Flowable
转换为 Single
与 cost.
@WorkerThread
Flowable<Integer> updateMovies(Helper help) {
return movieDao.getMovies()
.firstOrError()
.flatMapIterable(movies -> movies)
.flatMap(movie -> { ... })
.map(movie -> { ... });
}
尽管为了意图清楚起见,我更喜欢第一个选项。
我正在使用 rxjava & Room 尝试更新数据库中的行列表。 卡在事件不断触发的循环中
道
@Query("SELECT * FROM movies")
Flowable<List<Movie>> getMovies();
@Update
int updateMovie(Movie movie);
UpdateClass - 助手 classes 正在像这样更新 Db
// Trying to get all existing movies and update one value in all of them.
@WorkerThread
Flowable<Integer> updateMovies(Helper help) {
return movieDao.getMovies().flatMapIterable(movies -> movies)
.flatMap(movie -> {
LogUtils.debug("movieusecase", "movieid" + movie.getMovieId());
movie.updateRating(help.getUpdatedVal(movie));
return Flowable.just(movie);
}).map(movie -> {
return movieDao.updateMovie(movie);
});
}
一旦包含 updateMovie
调用,我就会陷入无限循环,重复事件不断来自房间 db。
Presenter Class - 调用更新 class 来更新数据库中的内容。在 activity onCreate
updateClass.updateMovies()
.observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(movie -> {
// update stuff
}, throwable -> {
LogUtils.error(TAG, throwable.getMessage());
});
在此先感谢任何帮助。
听起来像是 Single
而不是 Flowable
的工作。
不同之处在于 Single
只会对数据库中的当前电影列表触发一次,它不会像 Flowable
那样不断发出变化。
Single<List<Movie>> getMovies();
或者如果您出于某种原因希望您的方法 return Flowable
,您也可以使用 Flowable.firstOrError()
将您的 Flowable
转换为 Single
与 cost.
@WorkerThread
Flowable<Integer> updateMovies(Helper help) {
return movieDao.getMovies()
.firstOrError()
.flatMapIterable(movies -> movies)
.flatMap(movie -> { ... })
.map(movie -> { ... });
}
尽管为了意图清楚起见,我更喜欢第一个选项。