Room 数据库的 Drop 删除触发器
Drop delete trigger for Room database
我正在使用房间数据库来存储评论,并使用 RxJava 作为侦听器在数据库更改时做一些事情。
我不想在 table 上调用 delete 时调用回调,仅在调用 insert 时调用回调。
到目前为止我发现 Room 库有 triggers
在 table 的 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)
}
)
}
您可以通过过滤原始 getAll()
Flowable
获得仅在插入时发出而不在删除时发出的 Flowable<List<Comment>>
,以便仅传递那些 List<Comment>
项比之前的 List<Comment>
.
包含更多 Comment
您可以通过以下转换实现此过滤:
- 在可流动对象前加上一个空列表,这样我们就有了插入的基线。
- 获取尺寸为 2 的
RxJava
window()
s,以便我们能够比较相邻的项目。
window()
returnsFlowable<Flowable<Comment>>
。在内部 Flowable
. 上使用 flatMap()
和 toList()
将其转换为 Flowable<List<Comment>>
- 过滤那些代表插入的双元素windows(第一个元素的大小小于第二个元素的大小)。
- 仅发出过滤后的第二个元素 windows。
在 Kotlin 中:
fun getAllAfterInsertions() {
getAll()
.startWith(emptyList<String>()) // (1)
.window(2, 1) // (2)
.flatMap({ w -> w.toList().toFlowable() }) // (3)
.filter({ w -> w.size == 2 && w[0].size < w[1].size }) // (4)
.map({ window -> window[1] }) // (5)
}
要在没有通知的情况下删除,我只需替换
MyDao().delete()
其中一个执行@Query
MyDao().deleteLast()
然后 Flowable 不会发出新事件。 @Dao 看起来像这样
@Dao
abstract class MyDao : BaseDao<Data> {
@Query("DELETE FROM Data WHERE id = (select min(id) from Data)") // or something else
abstract fun deleteLast()
@Delete
fun delete(data: Data)
}
我正在使用房间数据库来存储评论,并使用 RxJava 作为侦听器在数据库更改时做一些事情。
我不想在 table 上调用 delete 时调用回调,仅在调用 insert 时调用回调。
到目前为止我发现 Room 库有 triggers
在 table 的 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)
}
)
}
您可以通过过滤原始 getAll()
Flowable
获得仅在插入时发出而不在删除时发出的 Flowable<List<Comment>>
,以便仅传递那些 List<Comment>
项比之前的 List<Comment>
.
Comment
您可以通过以下转换实现此过滤:
- 在可流动对象前加上一个空列表,这样我们就有了插入的基线。
- 获取尺寸为 2 的
RxJava
window()
s,以便我们能够比较相邻的项目。 window()
returnsFlowable<Flowable<Comment>>
。在内部Flowable
. 上使用 - 过滤那些代表插入的双元素windows(第一个元素的大小小于第二个元素的大小)。
- 仅发出过滤后的第二个元素 windows。
flatMap()
和 toList()
将其转换为 Flowable<List<Comment>>
在 Kotlin 中:
fun getAllAfterInsertions() {
getAll()
.startWith(emptyList<String>()) // (1)
.window(2, 1) // (2)
.flatMap({ w -> w.toList().toFlowable() }) // (3)
.filter({ w -> w.size == 2 && w[0].size < w[1].size }) // (4)
.map({ window -> window[1] }) // (5)
}
要在没有通知的情况下删除,我只需替换
MyDao().delete()
其中一个执行@Query
MyDao().deleteLast()
然后 Flowable 不会发出新事件。 @Dao 看起来像这样
@Dao
abstract class MyDao : BaseDao<Data> {
@Query("DELETE FROM Data WHERE id = (select min(id) from Data)") // or something else
abstract fun deleteLast()
@Delete
fun delete(data: Data)
}