Here's a critical point I don't know concerning the behavior of Picasso.
Imagine you are, say, showing a slide-show of ten items. Say, they are on-screen for ten seconds each.
The ideal behavior would be this: at the start of the slide show, I simply perform following:
picasso.get( url1 )
picasso.get( url2 )
picasso.get( url3 )
picasso.get( url4 )
picasso.get( url5 )
picasso.get( url6 )
picasso.get( url7 )
picasso.get( url8 )
picasso.get( url9 )
picasso.get( url10 )
And, in fact, Picasso would do those one at a time, in a queue.
What is the behavior of Picasso, if I tell it to pre-warm 10 urls all at once?
Is it possible to have Picasso do things only one at a time, in order - is there such an option?
(Other questions arising are, can you cancel the queue, or ...?)
Fresco
thanks to an amazing answer @alicanozkara on this page I learned for the first time about
https://github.com/facebook/fresco
(13k stars) for better or worse I think the Picasso era is probably over.
There is no one-method-call fix to chain with Picasso, but you could create a helper like follows:
Usage:
Please note, I've just written that from the top of my head while my IDE invalidates its caches, so you might need to tweak it a little
Using only
Picasso
, what I think you can achieve is:1) Load all the images asynchronously in the cache using
fetch()
like so:You can also add priority for images you want to load early: (Maybe mention high priority for first few images of the slide)
2) About cancelling the queue, you can add a common
tag()
to your images and you can pause/cancel/resume anytime!Then we can control the tag like so:
3) Another important thing I would like to suggest is when you are pre-loading your images, only save them into your disk-cache, and load them to your memory-cache only while displaying. It will prevent flushing other important images from the memory-cache:
4) For sequentially loading your images in a queue, you can pass your own
ExecutorService
(SingleThreadExecutor
in your case) usingexecutor(ExecutorService)
method, present inPicasso.Builder
You can even change the size of disk cache by using
downloader(Downloader)
method and your memory cache usingmemoryCache(Cache)
method, both found inPicasso.Builder
class.Other Awesome Libraries:
Glide
Fresco
I am not sure it can be done with Picasso itself, but at least RxJava may be applicable to this problem.
I'll post a snippet of code with comments:
This will be the output in logcat:
This is the ideal case scenario, when each image gets successfully loaded. This snippet doesn't handle the case, when one of images cannot be loaded. As soon as Picasso fails to load one of them - the stream will be interrupted and
onError()
would be called.In Picasso.Builder you can give a specific ExecutorService: https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/Picasso.Builder.html#executor-java.util.concurrent.ExecutorService-
If you give a new single thread executor, https://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor(), Picasso will download all your images one at a time.