'Glide won't load same url twice in a row with Volley integration while app is in foreground

I'm encountering a specific behavior I can't explain. I'm using Glide and Volley (for retries).

  1. I upload a profile picture and my server responds when the upload is complete.
  2. In the navigation bar, I use Glide to load the new profile picture successfully.
  3. I have an adapter which populates the UI for a bunch of posts containing the profile picture, BUT any post that tries to load the exact same url & signature() as step 2 just silently fails. The listener's onLoadFailed() or onResourceReady() never get called, and my server never gets a request for the profile picture. I am 100% sure I'm executing the Glide code to load the picture (I surrounded it with log statements which get executed after steps 1 and 2.) I also verified the url and signature in both steps are identical.

Observations:

  • If I change the signature in step 3, it still silently fails.

  • If I add skipMemoryCache(true) and diskCacheStrategy(DiskCacheStrategy.NONE) in step 3, it still silently fails.

  • If I refresh the page with the posts containing the profile pictures, the pictures still silently fail to load.

  • If I put the app in the background and immediately return to the app, Glide actually loads the images in step 3 by requesting it from my server! (It should get a cached version since the url & signature are the same)

  • If I comment out the Glide code in step 2, step 3 succeeds. Every post containing the profile picture executes the Glide code individually and succeeds!

  • If I completely close the app and reopen it, the profile picture in the navigation bar and the same profile picture in the posts also succeed in loading.

Here is the code that loads the profile picture in step 2:

Glide.with( MainActivity.this )
     .load( profile_pic_url )
     .signature( new ObjectKey(profile_pic_filename) )
     .into( profile_pic );

Here is my code for step 3 which is silently failing (the listener methods never execute).

Glide.with( context )
     .load( profile_pic_url )
     .dontAnimate()
     .signature( new ObjectKey(profile_pic_filename) )
     .listener( new RequestListener<Drawable>()
     {
         @Override
         public boolean onLoadFailed( @Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource )
         {
             Log.i( "piccc", "Load Failed: " + e );
             return false;
         }

         @Override
         public boolean onResourceReady( Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource )
         {
             Log.i( "piccc", "Resource Ready" );
             return false;
         }
     } )
     .into( profile_pic );

Here is my AppGlideModule where I define Glide's memory and disk caches and remove Volley's disk cache (I've messed around with how I'm caching to no avail):

@GlideModule
public class MyAppGlideModule extends AppGlideModule
{
    @Override
    public void applyOptions( Context context, GlideBuilder builder )
    {
        int memory_cache_size_bytes = 1024 * 1024 * 20; // 20 MB
        builder.setMemoryCache( new LruResourceCache(memory_cache_size_bytes) );

        int diskCacheSizeBytes = 1024 * 1024 * 400; // 400 MB
        builder.setDiskCache( new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes) );
    }

    @Override
    public void registerComponents( Context context, Glide glide, Registry registry )
    {
        RequestQueue queue = new RequestQueue( new NoCache(),
                                               new BasicNetwork(new HurlStack()), 16 ) 
        {
            @Override 
            public <T> Request<T> add( Request<T> request ) 
            {
                request.setRetryPolicy( new DefaultRetryPolicy(10000, 1, 1) );
                return super.add( request );
            }
        };

        queue.start();

        registry.replace( GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(queue) );
    }
}

build.gradle:

ext {
    glide_version = '4.13.0'
}

implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:annotations:$glide_version"
implementation "com.github.bumptech.glide:volley-integration:$glide_version";
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

Any help would be super appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source