【问题标题】:Drop delete trigger for Room database删除 Room 数据库的删除触发器
【发布时间】:2017-12-07 16:20:09
【问题描述】:

我正在使用房间数据库来存储 cmets 和 RxJava 作为侦听器,以便在数据库更改时做一些事情。

我不想在对表调用 delete 时调用回调,只有在调用 insert 时才调用。

到目前为止,我发现 Room 库有 triggers 被调用的表的 deleteinsertupdate 依次调用 RxJava 的方法。

有什么方法可以删除delete 触发器并仅获取insertupdate 方法的回调?

这是我的 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


    【解决方案1】:

    您可以通过过滤原始getAll() Flowable 来获得仅在插入时发出而不是在删除时发出的Flowable&lt;List&lt;Comment&gt;&gt;,这样只有那些List&lt;Comment&gt; 项目才能通过那些包含比以前更多Comments 的项目List&lt;Comment&gt;.

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

    1. 在 flowable 前面加上一个空列表,这样我们就有了插入的基线。
    2. 获取大小为2的RxJavawindow()s,这样我们就可以比较相邻的项目了。
    3. window() 返回 Flowable&lt;Flowable&lt;Comment&gt;&gt;。将其转换为 Flowable&lt;List&lt;Comment&gt;&gt;,并在内部 Flowable 上使用 flatMap()toList()
    4. 过滤那些代表插入的 2 元素窗口(第一个元素的大小小于第二个元素的大小)。
    5. 仅发射过滤窗口的第二个元素。

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

    【讨论】:

    • 这是正确的,抱歉这么久才回复。您介意再解释一下窗口函数是如何过滤掉插入的吗?
    【解决方案2】:

    要删除没有通知,我只需替换

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2019-12-19
      • 2021-12-24
      • 2012-06-29
      相关资源
      最近更新 更多