Efficient data loading in RecycleView

I fill out a list of data, and this data is loaded in 3 steps, so after the first step to finish, I already show the data to the user. Then I update the user interface each time the load state changes ...

I use RecyclerView to display my data. I could notice that when loading individual steps is very fast, the user interface is blocked (especially when the user is currently scrolling very quickly). Therefore, I need to group events and update only the user interface every x ms ...

When testing, I saw that updating the user interface every 150 ms seems good and fast and does not lead to a noticeable stutter.

But I'm not sure how old devices or other devices react.

Does anyone have experience in this area and can tell me what value will be good?

General question

Perhaps the question can be asked even more broadly: when updating the user interface very often, what is the best way to use to avoid blocking the user interface? Or what is the minimum wait time before updating the user interface again

+5
source share
2 answers

Your question is very interesting, and there are some solutions. I just posted some solutions here with a link.

1. Cache of your data

When the user scrolls, your adapter will request some data from the array, from the database or network. Due to poor performance, I think maybe a problem, because the data load time is long. Therefore, the best way to solve this problem is to cache data.

You can cache your data in memory using the LRUCache class and on disk using DiskLRUCache. Android manual exists for this problem: Caching a bitmap image

Psuedo Code:

 if (memoryCache.get(key)) { // load data from memory and assign to cell } else if (diskCache.get(key)) { // load data from disk and assign to cell } else { // get data from database or network // assign to cell // save to memory cache // save to disk cache } 

2. Use a different thread to load data

When you handle a long-term task affecting your user interface, you should put it in the user interface thread and create a new thread for your purpose. You can use Asynctask for this purpose.

The problem with this method is this: when loading data from the background, the displayed cell on the screen using the scroll event needs different data. That way, when the old asynctask object completed, it assigns the old data to your cell. BUMP !!! . Therefore, you must carefully monitor the condition of your camera. There is a good tutorial: Loading a bootable image into another stream

3. Simplify the presentation

If you are not using something like database processing, a network query while scrolling, perhaps the problem is that your view is too complicated. You should use this tool: Hierarchy Viewer to check which layout file has performance issues. You can also use the GPU drag and drop tool to verify that many parts of your view have made many things unnecessary.

4. Determine the time for updating the database by frame rate

You should always keep the frame rate below 60 frames per second. With this frame rate, the user will not recognize lag, awkward ... in your opinion. You can use the GPU Profiler to check the frame rate. Based on this frame rate, you decide that you should simplify viewing, optimize the onDraw method onDraw or optimize your graphic design (due to overuse) to maintain a user interface frame rate of <= 60 frames per second.

All the above methods that I included in my project called ImageUploader . This application uploads an image to Flickr and displays a list of all uploaded images or views individual images.

Google has downloaded the Android Performance Pattern series to teach you many useful things, such as caching, dosing, useful tips ...

Hope this help :)

+8
source

I know the post is old. But if someone still wants to deal efficiently with loading data into RecycleView, try this library https://github.com/mrHerintsoaHasina/picassiette . Similar to Picasso or Glide, but also supports user data (not just raster data). You must port your project to androidx.

0
source

Source: https://habr.com/ru/post/1241614/


All Articles