Skip to content

Commit

Permalink
Fix an issue with scale and coalesing
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 20, 2024
1 parent 192964e commit 2b55252
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/Nuke/Internal/ImageRequestKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ extension ImageRequest {
final class CacheKey: Hashable, Sendable {
// Using a reference type turned out to be significantly faster
private let imageId: String?
private let scale: Float
private let thumbnail: ImageRequest.ThumbnailOptions?
private let processors: [any ImageProcessing]

init(_ request: ImageRequest) {
self.imageId = request.preferredImageId
self.scale = request.scale ?? 1
self.thumbnail = request.thumbnail
self.processors = request.processors
}

func hash(into hasher: inout Hasher) {
hasher.combine(imageId)
hasher.combine(scale)
hasher.combine(thumbnail)
hasher.combine(processors.count)
}

static func == (lhs: CacheKey, rhs: CacheKey) -> Bool {
lhs.imageId == rhs.imageId && lhs.thumbnail == rhs.thumbnail && lhs.processors == rhs.processors
lhs.imageId == rhs.imageId && lhs.scale == rhs.scale && lhs.thumbnail == rhs.thumbnail && lhs.processors == rhs.processors
}
}

Expand Down Expand Up @@ -86,10 +89,12 @@ final class ImageLoadKey: Hashable, Sendable {
/// Uniquely identifies a task of retrieving the decoded image.
struct DecodedImageLoadKey: Hashable {
let dataLoadKey: DataLoadKey
let scale: Float
let thumbnail: ImageRequest.ThumbnailOptions?

init(_ request: ImageRequest) {
self.dataLoadKey = DataLoadKey(request)
self.scale = request.scale ?? 1
self.thumbnail = request.thumbnail
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ class ImagePipelineCoalescingTests: XCTestCase {
}
}

// MARK: - Scale

func testOverridingImageScale() throws {
dataLoader.queue.isSuspended = true

// GIVEN requests with the same URLs but one accesses thumbnail
let request1 = ImageRequest(url: Test.url, userInfo: [.scaleKey: 2])
let request2 = ImageRequest(url: Test.url, userInfo: [.scaleKey: 3])

// WHEN loading images for those requests
expect(pipeline).toLoadImage(with: request1) { result in
// THEN
guard let image = result.value?.image else { return XCTFail() }
XCTAssertEqual(image.scale, 2)
}
expect(pipeline).toLoadImage(with: request2) { result in
// THEN
guard let image = result.value?.image else { return XCTFail() }
XCTAssertEqual(image.scale, 3)
}

dataLoader.queue.isSuspended = false

wait()

XCTAssertEqual(self.dataLoader.createdTaskCount, 1)
}

// MARK: - Thumbnail

func testDeduplicationGivenSameURLButDifferentThumbnailOptions() {
Expand Down

0 comments on commit 2b55252

Please sign in to comment.