Skip to content

Commit

Permalink
Add completion handler for web url not matched
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed May 9, 2017
1 parent ee338ca commit 5c24f35
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Sources/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ extension Kingfisher where Base: ImageView {
completionHandler: {[weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
guard let strongBase = base, imageURL == self.webURL else {
completionHandler?(image, error, cacheType, imageURL)
return
}

self.setImageTask(nil)
guard let image = image else {
maybeIndicator?.stopAnimatingView()
Expand Down
9 changes: 5 additions & 4 deletions Sources/NSButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ extension Kingfisher where Base: NSButton {
completionHandler: {[weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
guard let strongBase = base, imageURL == self.webURL else {
completionHandler?(image, error, cacheType, imageURL)
return
}
self.setImageTask(nil)

if image != nil {
strongBase.image = image
}
Expand Down Expand Up @@ -156,16 +158,15 @@ extension Kingfisher where Base: NSButton {
completionHandler: {[weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
guard let strongBase = base, imageURL == self.alternateWebURL else {
completionHandler?(image, error, cacheType, imageURL)
return
}
self.setAlternateImageTask(nil)

guard let image = image else {
completionHandler?(nil, error, cacheType, imageURL)
return
if image != nil {
strongBase.alternateImage = image
}

strongBase.alternateImage = image
completionHandler?(image, error, cacheType, imageURL)
}
})
Expand Down
3 changes: 2 additions & 1 deletion Sources/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ extension Kingfisher where Base: UIButton {
completionHandler: {[weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
guard let strongBase = base, imageURL == self.webURL(for: state) else {
completionHandler?(image, error, cacheType, imageURL)
return
}
self.setImageTask(nil)

if image != nil {
strongBase.setImage(image, for: state)
}
Expand Down Expand Up @@ -163,6 +163,7 @@ extension Kingfisher where Base: UIButton {
completionHandler: { [weak base] image, error, cacheType, imageURL in
DispatchQueue.main.safeAsync {
guard let strongBase = base, imageURL == self.backgroundWebURL(for: state) else {
completionHandler?(image, error, cacheType, imageURL)
return
}
self.setBackgroundImageTask(nil)
Expand Down
38 changes: 36 additions & 2 deletions Tests/KingfisherTests/ImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class ImageViewExtensionTests: XCTestCase {
}

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(Double(NSEC_PER_SEC) * 0.1)) / Double(NSEC_PER_SEC)) { () -> Void in
XCTAssertFalse(task1Complete, "Task 1 should not be completed since task 2 overrides it.")
XCTAssertTrue(task1Complete, "Task 1 should be completed even task 2 overrides it. See issue 665.")
XCTAssertTrue(task2Complete, "Task 2 should be completed.")

XCTAssertFalse(task1Progress, "Progress of Task 1 should not be called since task 2 overrides it.")
Expand Down Expand Up @@ -595,6 +595,40 @@ class ImageViewExtensionTests: XCTestCase {


waitForExpectations(timeout: 5, handler: nil)

}

// https://github.com/onevcat/Kingfisher/issues/665
// The completion handler should be called even when the image view loading url gets changed.
func testIssue665() {
let expectation = self.expectation(description: "wait for downloading image")

let URLString1 = testKeys[0]
let URLString2 = testKeys[1]

_ = stubRequest("GET", URLString1).andReturn(200)?.withBody(testImageData)
_ = stubRequest("GET", URLString2).andReturn(200)?.withBody(testImageData)

let url1 = URL(string: URLString1)!
let url2 = URL(string: URLString2)!

var url1Completed = false
var url2Completed = false

imageView.kf.setImage(with: url1) { (image, _, cacheType, url) in
url1Completed = true
}

imageView.kf.setImage(with: url2) { (image, _, cacheType, url) in
url2Completed = true
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
XCTAssertTrue(url1Completed)
XCTAssertTrue(url2Completed)

expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
}
}

0 comments on commit 5c24f35

Please sign in to comment.