Search code examples
iosimagecachingsdwebimage

Prefetched SDWebImage is not persisted after closing app


I am using SDWebImage prefetch i.e

SDWebImagePrefetcher
      .shared()
      .prefetchURLs(
        urls as [URL],
        progress: nil,
        completed: { finished, skipped in
          onComplete([finished, skipped])
        }
      )

Everything seems fine, all expected images are cached and feel responsive when I use my app. I am showing a loader while fetching these images, it takes about 4 - 5s to complete.

I expected that next time my app is opened this prefetch will be instant since images should already be in the cache? However if I completely quit my app (close it from iOS active app view etc...) and then open it, I have to wait same 4 - 5s again.

Am I miss-understanding what this method does and perhaps there is another solution that would behave how I expect it?


Solution

  • Try to force the cache type to disk, from the sources:

    /**
     A SDImageCacheType raw value which specify the cache type when the image has just been downloaded and will be stored to the cache. Specify `SDImageCacheTypeNone` to disable cache storage; `SDImageCacheTypeDisk` to store in disk cache only; `SDImageCacheTypeMemory` to store in memory only. And `SDImageCacheTypeAll` to store in both memory cache and disk cache.
     If not provide or the value is invalid, we will use `SDImageCacheTypeAll`. (NSNumber)
     */
    SDWebImageContextOption _Nonnull const SDWebImageContextStoreCacheType;
    

    Link here: https://github.com/SDWebImage/SDWebImage/blob/5.x/SDWebImage/SDWebImageDefine.h#L204-L208