Fix the edge cases when transformer and thumbnail get applied at the same time, we need to write correct code to query memory cache only

This commit is contained in:
DreamPiggy 2020-04-01 11:22:22 +08:00
parent aafa7a2222
commit ffeea1a334
1 changed files with 23 additions and 8 deletions

View File

@ -116,15 +116,30 @@ public final class ImageManager : ObservableObject {
if let result = manager.optionsProcessor?.processedResult(for: url, options: options, context: context) {
context = result.context
}
// TODO: Remove transformer for cache calculation before SDWebImage 5.7.0, this is bug. Remove later
let transformer = (context?[.imageTransformer] as? SDImageTransformer) ?? manager.transformer
context?[.imageTransformer] = nil
// TODO: before SDWebImage 5.7.0, this is the SPI. Remove later
let key = manager.perform(Selector(("cacheKeyForURL:context:")), with: url, with: context)?.takeUnretainedValue() as? String
// This callback is synchronzied
manager.imageCache.containsImage(forKey: key, cacheType: .memory) { [unowned self] (cacheType) in
if cacheType == .memory {
self.manager.imageCache.queryImage(forKey: key, options: options, context: context) { [unowned self] (image, data, cacheType) in
self.image = image
if let image = image {
self.successBlock?(image, cacheType)
var key = manager.perform(Selector(("cacheKeyForURL:context:")), with: url, with: context)?.takeUnretainedValue() as? String
if let transformer = transformer {
key = SDTransformedKeyForKey(key, transformer.transformerKey)
}
// Shortcut for built-in cache
if let imageCache = manager.imageCache as? SDImageCache {
let image = imageCache.imageFromMemoryCache(forKey: key)
self.image = image
if let image = image {
self.successBlock?(image, .memory)
}
} else {
// This callback is synchronzied
manager.imageCache.containsImage(forKey: key, cacheType: .memory) { [unowned self] (cacheType) in
if cacheType == .memory {
self.manager.imageCache.queryImage(forKey: key, options: options, context: context) { [unowned self] (image, data, cacheType) in
self.image = image
if let image = image {
self.successBlock?(image, cacheType)
}
}
}
}