@@ -110,45 +110,19 @@ public final class ImageManager : ObservableObject {
110110 /// Prefetch the initial state of image, currently query the memory cache only
111111 func prefetch( ) {
112112 isFirstPrefetch = false
113- // Use the options processor if provided
114- let options = self . options
115- var context = self . context
116- if let result = manager. optionsProcessor? . processedResult ( for: url, options: options, context: context) {
117- context = result. context
118- }
119- // TODO: Remove transformer for cache calculation before SDWebImage 5.7.0, this is bug. Remove later
120- let transformer = ( context ? [ . imageTransformer] as? SDImageTransformer ) ?? manager. transformer
121- context ? [ . imageTransformer] = nil
122- // TODO: before SDWebImage 5.7.0, this is the SPI. Remove later
123- var key : String ?
124- let selector = Selector ( ( " cacheKeyForURL:context: " ) )
125- if manager. responds ( to: selector) {
126- key = manager. perform ( selector, with: url, with: context) ? . takeUnretainedValue ( ) as? String
127- } else {
128- key = manager. cacheKey ( for: url)
129- }
130- if let transformer = transformer {
131- key = SDTransformedKeyForKey ( key, transformer. transformerKey)
113+ var options = self . options
114+ if options. contains ( . fromLoaderOnly) {
115+ // If user indeed ignore cache, don't do prefetch
116+ return
132117 }
133- // Shortcut for built-in cache
134- if let imageCache = manager. imageCache as? SDImageCache {
135- let image = imageCache. imageFromMemoryCache ( forKey: key)
118+ // Use `.fromCacheOnly` to query cache only
119+ options. insert ( . fromCacheOnly)
120+ var context = self . context ?? [ : ]
121+ context [ . queryCacheType] = SDImageCacheType . memory. rawValue
122+ // Use `.queryCacheType` to query memory cache only
123+ manager. loadImage ( with: url, options: options, context: context, progress: nil ) { ( image, data, error, cacheType, finished, imageUrl) in
124+ // This will callback immediately
136125 self . image = image
137- if let image = image {
138- self . successBlock ? ( image, . memory)
139- }
140- } else {
141- // This callback is synchronzied
142- manager. imageCache. containsImage ( forKey: key, cacheType: . memory) { [ unowned self] ( cacheType) in
143- if cacheType == . memory {
144- self . manager. imageCache. queryImage ( forKey: key, options: options, context: context) { [ unowned self] ( image, data, cacheType) in
145- self . image = image
146- if let image = image {
147- self . successBlock ? ( image, cacheType)
148- }
149- }
150- }
151- }
152126 }
153127 }
154128
0 commit comments