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?