Skip to content

Commit

Permalink
Fix an issue with ImageRequest where setting a default processor as c…
Browse files Browse the repository at this point in the history
…ustom one would result in a CacheKey different from the default one
  • Loading branch information
kean committed Jul 29, 2018
1 parent 721286c commit 2475287
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
27 changes: 14 additions & 13 deletions Sources/ImageRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public struct ImageRequest {
get {
// Default processor on macOS is nil, on other platforms is Decompressor
#if !os(macOS)
return _ref._isDefaultProcessorUsed ? Container.decompressor : _ref._customProcessor
return _ref._isDefaultProcessorUsed ? ImageRequest.decompressor : _ref._processor
#else
return _ref._isDefaultProcessorUsed ? nil : _ref._customProcessor
return _ref._isDefaultProcessorUsed ? nil : _ref._processor
#endif
}
set {
_mutate {
$0._isDefaultProcessorUsed = false
$0._customProcessor = newValue
$0._processor = newValue
}
}
}
Expand Down Expand Up @@ -159,6 +159,8 @@ public struct ImageRequest {
self.processor = AnyImageProcessor(ImageDecompressor(targetSize: targetSize, contentMode: contentMode))
}

fileprivate static let decompressor = AnyImageProcessor(ImageDecompressor())

#endif

// CoW:
Expand All @@ -178,9 +180,10 @@ public struct ImageRequest {
var resource: Resource
var _urlString: String? // memoized absoluteString
// true unless user set a custom one, this allows us not to store the
// default processor anywhere in the `Container`.
// default processor anywhere in the `Container` & skip equality tests
// when the default processor is used
var _isDefaultProcessorUsed: Bool = true
var _customProcessor: AnyImageProcessor?
var _processor: AnyImageProcessor?
var memoryCacheOptions = MemoryCacheOptions()
var priority: ImageRequest.Priority = .normal
var cacheKey: AnyHashable?
Expand All @@ -196,18 +199,14 @@ public struct ImageRequest {
init(container ref: Container) {
self.resource = ref.resource
self._urlString = ref._urlString
self._customProcessor = ref._customProcessor
self._isDefaultProcessorUsed = ref._isDefaultProcessorUsed
self._processor = ref._processor
self.memoryCacheOptions = ref.memoryCacheOptions
self.priority = ref.priority
self.cacheKey = ref.cacheKey
self.loadKey = ref.loadKey
self.userInfo = ref.userInfo
}

#if !os(macOS)
fileprivate static let decompressor = AnyImageProcessor(ImageDecompressor())
#endif
}

/// Resource representation (either URL or URLRequest).
Expand Down Expand Up @@ -273,9 +272,11 @@ internal extension ImageRequest {
if let lhsCustomKey = lhs._ref.cacheKey, let rhsCustomKey = rhs._ref.cacheKey {
return lhsCustomKey == rhsCustomKey
}
return lhs._ref._urlString == rhs._ref._urlString
&& lhs._ref._isDefaultProcessorUsed == rhs._ref._isDefaultProcessorUsed
&& lhs._ref._customProcessor == rhs._ref._customProcessor
guard lhs._ref._urlString == rhs._ref._urlString else {
return false
}
return (lhs._ref._isDefaultProcessorUsed && rhs._ref._isDefaultProcessorUsed)
|| (lhs.processor == rhs.processor)
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/ImageRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class ImageRequestCacheKeyTests: XCTestCase {
AssertHashableEqual(CacheKey(request: request1), CacheKey(request: request2))
}

func testSettingDefaultProcessorManually() {
let request1 = ImageRequest(url: Test.url)
let request2 = ImageRequest(url: Test.url).mutated {
$0.processor = request1.processor
}
AssertHashableEqual(CacheKey(request: request1), CacheKey(request: request2))
}

// MARK: Custom Cache Key

func testRequestsWithSameCustomKeysAreEquivalent() {
Expand Down

0 comments on commit 2475287

Please sign in to comment.