Hello there!
I'm having a weird issue... Some images will not load while being offline even if they are cached.
Only after turning online the images will appear - but with a blue indicator (disk).
This happens only for a few images...
Picasso.with(context).load(xxx.getUrl()).fit().centerCrop().networkPolicy(NetworkPolicy.OFFLINE).into(holder.imgFrameBackground, new Callback()
{
@Override
public void onSuccess()
{
}
@Override
public void onError()
{
Picasso.with(context).load(xxx.getUrl()).fit().centerCrop().into(holder.imgFrameBackground);
}
});
The onError callback is triggered for the images that are not loaded.
With the approach above the images will load - but I have problems with the first and only the first image of a gridview. I already saw other similar problems on stackoverflow that local cached images are not loaded without the OFFLINE flag.
Also I saw that the first image of the appears multiple times inside the Picasso log. Picasso line is called once only for sure.
Some images will not load while being offline even if they are cached.
Only after turning online the images will appear - but with a blue indicator (disk).
This image is likely conditionally cached which requires an HTTP request to the remote server which returns a 304 allowing us to use the disk cached version. Without internet connection, the freshness of the locally cached image cannot be verified and thus the underlying HTTP client returns an error.
Also I saw that the first image of the appears multiple times inside the Picasso log.
This is how AdapterView
works, sadly. The first view will have it's getView
bound multiple times and your code needs to be able to handle that. Picasso has no issue with this, subsequent calls for the same view will cancel the previous ones, you can see this in its own sample.
Thank you, why does it work with NetworkPolicy OFFLINE then ?
Because it requests the image locally from the cache, even if the HTTP
client knows that it is stale.
On Mon, Sep 21, 2015, 9:50 PM Patric Corletto [email protected]
wrote:
Thank you, why does it work with NetworkPolicy OFFLINE then ?
—
Reply to this email directly or view it on GitHub
https://github.com/square/picasso/issues/1145#issuecomment-142157392.
So there should probably be a NetworkPolicy that allows taking it offline if available and otherwise do a network requests. I saw tons of people reporting this issue. And the workaround is pretty annoying.
+1
There should be a network policy which goes to disk cache and then only looks online. The only workaround which forces to look only offline first and then on error look online is a bit ugly. Especially considering the sleekness of Picasso otherwise.
One alternative could be to just load the stale image while looking for live one in the background. Right now if my internet connection is down the image loads. But if Picasso is looking for it online the ImageView remains blank while it does (even if a stale or sometimes not so stale version is available in disk cache).
This logic worked for me:
if network is available:
Picasso.with(context).load(image).into(imageView);
else:
Picasso.with(context).networkPolicy(NetworkPolicy.OFFLINE).load(image).into(imageView);
Most helpful comment
+1
There should be a network policy which goes to disk cache and then only looks online. The only workaround which forces to look only offline first and then on error look online is a bit ugly. Especially considering the sleekness of Picasso otherwise.
One alternative could be to just load the stale image while looking for live one in the background. Right now if my internet connection is down the image loads. But if Picasso is looking for it online the ImageView remains blank while it does (even if a stale or sometimes not so stale version is available in disk cache).