3

I am trying to preload and cache these images to disk with Glide on app start. My preloading code looks like this:

getAllImageUrls().forEach { url -> GlideApp.with(context)
        .load(url)
        .apply(RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
        // request listener added for debug process
        .listener(object: RequestListener<Drawable> {

            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {

                ...

                return true
            }

            override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {

                // logging onResourceReady and data source here

                return true
            }
        })
        .signature(IntegerVersionSignature(version))
        // preloads image into cache using original dimensions
        .preload()}

To load images into an image view my code looks like this:

fun ImageView.loadImageFromUrl(imageUrl: String, activity: Activity, useCrossFade: Boolean = DEFAULT_CROSS_FADE_SETTING) {
GlideApp.with(activity).loadWithDefaultConfigInto(imageUrl, this, useCrossFade)}

private fun RequestManager.loadWithDefaultConfigInto(imageUrl: String, imageView: ImageView, useCrossFade: Boolean) {

    val transitionOptions: DrawableTransitionOptions = if (useCrossFade) DrawableTransitionOptions.withCrossFade() else DrawableTransitionOptions().dontTransition()

    this.load(imageUrl)
            .apply(RequestOptions().diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY_GLIDE))
            .transition(transitionOptions)
            .into(imageView)
}

But apparently the disk caching of images is not working. Because when I check the network request via Charles I am seeing all image urls being loaded on app start, but than on different screens when these preloaded images are needed the app is hitting the network again and loading the same image url for a second time.

What do I need to do to get preloading and disk caching working?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rik van Velzen
  • 1,977
  • 1
  • 19
  • 37
  • Is your device have any shortage of memory?? How much free GB left in your device internal storage! – Xenolion Oct 19 '17 at 14:02

1 Answers1

2

This is a very old question, but here it goes anyway:

You seem to be using a signature on the preloading and not when you load the images after that, so they won't hit the cache.

lbarbosa
  • 2,042
  • 20
  • 23