Search code examples
androidpicasso

Load images in background thread or UI thread?


My android app has a ListView which fetches a set of image urls from the web in the background and then passes this to the UI thread. The UI thread then downloads the images and displays them (I'm using Picasso for the same to ensure caching and resizing.)

I would like to know if I should download the images in the background thread and then pass only the resized bitmaps to the UI thread for display. Also, what happens when the user scrolls the screen in both the scenarios - do all images get retrieved from the web again? I'm not able to understand which would be faster in this case. The app is very similar to the Facebook android app.

One more thing - My app would refresh every 5 minutes and if there is any new image url in the list, it would restart the entire process starting from the background thread. My idea is that since the earlier images would have cached, they will not be loaded again. Only the new urls will be loaded. Is this correct?


Solution

  • The UI thread then downloads the images and displays them (I'm using Picasso for the same to ensure caching and resizing.)

    Picasso does not download the images on the main application thread by default. I'm not even sure that there is an option to do that.

    I would like to know if I should download the images in the background thread and then pass only the resized bitmaps to the UI thread for display.

    Picasso should be doing that for you.

    Also, what happens when the user scrolls the screen in both the scenarios - do all images get retrieved from the web again?

    That depends on how many images, how big they are, the size of the cache, and so forth. Picasso gives you options for determining how this is behaving, from debug indicators in the corners to indicate the image source (setIndicatorsEnabled(true)) to getting a StatsSnapshot to tell you how your caching is performing (getSnapshot()).