问题
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