Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading data effectively in RecycleView

I'm populating a list of data and this data is loaded in 3 steps, so after the first step is finsihed I already show the data to the user. Then I update the UI step by step whenever the data loading state changes...

I use a RecyclerView to display my data. I could observe, that when the loading of the single steps is very fast, the UI is blocking (especially when the user is currently scrolling very fast)... So I need to group the events and only update the UI every x ms...

With testing I saw, that updating the UI every 150ms seems to look good and fast and does not lead to any visible stuttering.

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

Does anyone have any experience in this direction and can tell me which value would be good?

General question

Probably the question can be asked even more generally: when updating the UI very frequently, what's the best frequence to use to avoid blocking UI? Or what's the minimum time to wait before updating the UI again

like image 857
prom85 Avatar asked Jan 27 '16 13:01

prom85


1 Answers

Your question is very interesting and there are some methods for solving. I just put some solutions here with reference.

1. Cache your data

When user scroll, your adapter will query some data from array, from database or network. Due to your slow performance, I guess maybe a problem because long loading data time. So the best method for solving this problem is caching data.

You can cache your data on memory using LRUCache class and on disk by using DiskLRUCache There is a Android Guideline for this problem: Caching bitmap

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 different thread for loading data

When you process a long running task affect your UI, you should put off to UI Thread and create new thread for your purpose. You can use Asynctask for this purpose.

The problem in this method is: while loading data from background, the displayed cell on screen by scroll event need different data. So, when old asynctask object finished work, it assigns old data to your cell. BUMP!!!. So you must control your cell's state carefully. There is a nice tutorial: Process Image Loading In Different Thread

3. Simplify your View

If you don't use something such as database handling, network request while scrolling, maybe the problem is your view is too complex. You should use this tool:Hierarchy Viewer for checking which your layout file has performance problem. You can also use tool GPU Overdraw tool for checking if many parts on your view has drawn many thing unnecessary.

4. Detect time for updating data base on frame rate

You must always keep your frame rate below 60fps. With that frame rate, user will not recognize lag, clunky ... in your view. You can use GPU Profiler for checking frame rate. Base on that frame rate, you will decide you should simplify view, optimize onDraw method or optimize GPU Drawing (due to overdrawing) for keeping your UI frame rate <= 60fps.

All above methods I have integrated to my project named ImageUploader . This application uploads image to Flickr service and display list of all uploaded images or view single images.

Google has uploaded a series of Android Performance Pattern teach you many useful things such as caching, batching, useful tips ...

Hope this help :)

like image 174
hqt Avatar answered Nov 11 '22 02:11

hqt