删除会议室数据库的删除触发器 [英] Drop delete trigger for Room database

查看:69
本文介绍了删除会议室数据库的删除触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用会议室数据库存储评论,而RxJava作为侦听器在数据库更改时会做一些事情.

I am using room database to store comments and RxJava as a listener to do some stuff when the database is changed.

我不希望仅在调用insert时才在表上调用delete时不调用回调.

I want to not call the callback when delete is called on the table, only when insert is called.

到目前为止,我发现Room库具有triggers,它们分别在表的deleteinsertupdate上调用,依次调用RxJava的方法.

What i found out so far is that Room library has triggers that are called on delete, insert and update of the table that in turn call RxJava's methods.

是否有任何方法可以删除delete触发器并仅获取insertupdate方法的回调?

Is there any way to drop the delete trigger and get callbacks only for the insert and update methods?

这是我的CommentDAO:

Here is my CommentDAO:

@Query("SELECT * FROM comments" )
fun getAll(): Flowable<List<Comment>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(comment: Comment)

@Delete
fun delete(comment: Comment)

以及我的RxJava回调函数:

And my RxJava callback functions:

 /**
 * 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>通过的项目中包含的Comment比以前的List<Comment>多.

You can get a Flowable<List<Comment>> that only emits on insertions and not on deletions by filtering the original getAll() Flowable so that only those List<Comment> items are passed through that contain more Comments than the previous List<Comment>.

您可以通过以下转换来实现此过滤:

You can implement this filtering with the following transformations:

  1. 在可流动对象的前面加上一个空列表,以便为插入创建基线.
  2. 获取大小为2的RxJava window(),这样我们就可以比较相邻的项目.
  3. window()返回Flowable<Flowable<Comment>>.使用内部Flowable上的flatMap()toList()将其转换为Flowable<List<Comment>>.
  4. 过滤那些表示插入的2元素窗口(第一个元素的大小小于第二个元素的大小).
  5. 仅发出已过滤窗口的第二个元素.
  1. Prepend the flowable with an empty list so that we have a baseline for insertions.
  2. Get RxJava window()s of size 2, so that we will be able to compare adjacent items.
  3. window() returns Flowable<Flowable<Comment>>. Convert it to Flowable<List<Comment>> with flatMap() and toList() on the inner Flowable.
  4. Filter those 2-element windows that represent an insertion (the size of the first element is less than the size of the second).
  5. Emit only the 2nd element of the filtered windows.

在科特林:

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)
}

这篇关于删除会议室数据库的删除触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆