How can I selectively observe Android Room database INSERT, DELETE, and UPDATE events?

前提是你 提交于 2019-12-08 11:26:26

问题


I have DAO designed this way.

@Dao
interface FooDao {

    @Query("SELECT * FROM foo")
    LiveData<List<Foo>> stream();
}

The above lets me receive INSERT, UPDATE, and DELETE events all at once, which I have no way of knowing whether the data is recently inserted, deleted, or being updated.

Goal

My goal is to be able to update the database rapidly and have RecyclerView reflect the changes without stuttering or completely freezing.

Current situation

I have a background service that's updating the database and using the above method, I'm using the DiffUtil class to calculate the difference and update the list accordingly on the front end.

Issue

Because updates are coming in so fast, the RecyclerView gets frozen up. I added background processing (DiffUtil calculation) using RxJava (since I have it in there anyway), which only reduced the stutter significantly though not completely.

Ideal goal

I need to figure out a way to update the list individually or if at all possible, skip receiving only UPDATE events from Room leaving out INSERT and DELETE events since these are done manually so should not be able to freeze the UI.

That way, I can use EventBus to notify the adapter that a row has changed from the background service without triggering UPDATE events from Room, which would have otherwise caused the adapter to perform the DiffUtil calculation, therefore slowing the UI down.

This means isolating database events for INSERT, DELETE, and UPDATE or selectively observing specific events only.

Is there such a way with Room and if so, how can I achieve this?

来源:https://stackoverflow.com/questions/57948812/how-can-i-selectively-observe-android-room-database-insert-delete-and-update-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!