Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some offline cached images not loading offline #1145

Closed
paatz04 opened this issue Sep 21, 2015 · 6 comments
Closed

Some offline cached images not loading offline #1145

paatz04 opened this issue Sep 21, 2015 · 6 comments

Comments

@paatz04
Copy link

paatz04 commented Sep 21, 2015

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.

@JakeWharton
Copy link
Member

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.

@paatz04
Copy link
Author

paatz04 commented Sep 22, 2015

Thank you, why does it work with NetworkPolicy OFFLINE then ?

@JakeWharton
Copy link
Member

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 notifications@github.com
wrote:

Thank you, why does it work with NetworkPolicy OFFLINE then ?


Reply to this email directly or view it on GitHub
#1145 (comment).

@paatz04
Copy link
Author

paatz04 commented Sep 23, 2015

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.

@gkgupta
Copy link

gkgupta commented Nov 13, 2015

+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).

@jrejaud
Copy link

jrejaud commented Jul 1, 2016

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants