【发布时间】:2017-12-07 16:20:09
【问题描述】:
我正在使用房间数据库来存储 cmets 和 RxJava 作为侦听器,以便在数据库更改时做一些事情。
我不想在对表调用 delete 时调用回调,只有在调用 insert 时才调用。
到目前为止,我发现 Room 库有 triggers 被调用的表的 delete、insert 和 update 依次调用 RxJava 的方法。
有什么方法可以删除delete 触发器并仅获取insert 和update 方法的回调?
这是我的 CommentDAO:
@Query("SELECT * FROM comments" )
fun getAll(): Flowable<List<Comment>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(comment: Comment)
@Delete
fun delete(comment: Comment)
还有我的 RxJava 回调函数:
/**
* Inserts comment into comment database
*
* @param object that's going to be inserted to the database
*/
fun saveComment(comment: Comment) {
Observable.just(comment).subscribeOn(Schedulers.io()).map({ comment1 -> commentdb.commentDao().insert(comment1) }).subscribe()
}
/**
* Removes comment from the database
*
* @param comment object that's going to be removed
*/
fun removeComment(comment: Comment){
Observable.just(comment).subscribeOn(Schedulers.io()).map({ comment1 -> commentdb.commentDao().delete(comment1) }).subscribe()
}
fun createCommentObservable(uploader: CommentUploader) {
commentdb.commentDao().getAll().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
{
success -> uploader.queue(success)
}
)
}
【问题讨论】:
标签: android kotlin android-room rx-android android-architecture-components