I am using room database to store comments and RxJava as a listener to do some stuff when the database is changed.
I want to not call the callback when delete is called on the table, only when insert is called.
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.
Is there any way to drop the delete
trigger and get callbacks only for the insert
and update
methods?
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)
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)
}
)
}
To delete without notification I simply replace
MyDao().delete()
with one executing a @Query
MyDao().deleteLast()
then thew Flowable doesn't emit a new event. The @Dao looks like this
You can get a
Flowable<List<Comment>>
that only emits on insertions and not on deletions by filtering the originalgetAll()
Flowable
so that only thoseList<Comment>
items are passed through that contain moreComment
s than the previousList<Comment>
.You can implement this filtering with the following transformations:
RxJava
window()
s of size 2, so that we will be able to compare adjacent items.window()
returnsFlowable<Flowable<Comment>>
. Convert it toFlowable<List<Comment>>
withflatMap()
andtoList()
on the innerFlowable
.In Kotlin: