N
N
Nikita Shelkovnikov2020-06-01 16:19:39
Android
Nikita Shelkovnikov, 2020-06-01 16:19:39

How to avoid RecyclerView re-rendering too often when using Flowable (RxJava)?

Greetings.

Implementing MVVM with room + flowable from rxjava + retrofit2.

When I open an activity from the repository, I get a FLowable with a list of items from the database (room).
At the same moment, the repository requests the list from the API, as soon as the result is returned, I delete the existing entries in the database and insert what came from the API.

The observer in the activity, of course, manages to render the list in the recyclerview three times. At the same time, blinking is noticeable: the list is not empty -> the list is empty -> the list is not empty.

Having received once a flowable from a room with a list of elements, the observer then receives an empty list (when deleting records in the database) and then a new list after inserting it into the database.

How to avoid re-rendering the RecyclerView too often in this case?
Is there any well-established best practice, true wei, etc.?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WelloGraphics, 2020-06-11
@sholkyman

You can try wrapping the removal of elements and the insertion of elements into a transaction so that this intermediate stage

list is not empty -> list is empty -> list is not empty
gone.
You can do it somehow like this:
@Dao
abstract class ItemDao {
    @Transaction
    public void replaceItems(List<Item> items) {
         clearTable();
         insert(items);
    }

    @Insert
    protected abstract void insert(List<Item> items);

    @Query("DELETE FROM Item")
    protected abstract void clearTable();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question