Skip to content

Commit

Permalink
[Issue #17] Duplicate image requests no longer cancel the active requ…
Browse files Browse the repository at this point in the history
…est.
  • Loading branch information
cnoon committed Sep 23, 2015
1 parent aa7e14e commit bc0e4fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Source/UIImageView+AlamofireImage.swift
Expand Up @@ -292,6 +292,8 @@ extension UIImageView {
imageTransition: ImageTransition,
completion: ((NSURLRequest?, NSHTTPURLResponse?, Result<UIImage>) -> Void)?)
{
guard !isURLRequestURLEqualToActiveRequestURL(URLRequest) else { return }

af_cancelImageRequest()

let imageDownloader = UIImageView.af_sharedImageDownloader
Expand Down Expand Up @@ -319,14 +321,7 @@ extension UIImageView {
filter: filter,
completion: { [weak self] request, response, result in
guard let strongSelf = self else { return }

// Ignore the event if the response request does not match the original request
guard let
currentRequest = strongSelf.af_activeRequest?.task.originalRequest
where currentRequest.URLString == request?.URLString else
{
return
}
guard strongSelf.isURLRequestURLEqualToActiveRequestURL(request) else { return }

strongSelf.af_activeRequest = nil

Expand Down Expand Up @@ -368,4 +363,15 @@ extension UIImageView {

return mutableURLRequest
}

private func isURLRequestURLEqualToActiveRequestURL(URLRequest: URLRequestConvertible?) -> Bool {
if let
currentRequest = af_activeRequest?.task.originalRequest
where currentRequest.URLString == URLRequest?.URLRequest.URLString
{
return true
}

return false
}
}
20 changes: 20 additions & 0 deletions Tests/UIImageViewTests.swift
Expand Up @@ -96,6 +96,26 @@ class UIImageViewTestCase: BaseTestCase {
XCTAssertTrue(imageDownloadComplete, "image download complete should be true")
}

func testThatImageDownloadSucceedsWhenDuplicateRequestIsSentToImageView() {
// Given
let expectation = expectationWithDescription("image should download successfully")
var imageDownloadComplete = false

let imageView = TestImageView {
imageDownloadComplete = true
expectation.fulfill()
}

// When
imageView.af_setImageWithURL(URL)
imageView.af_setImageWithURL(URL)
waitForExpectationsWithTimeout(timeout, handler: nil)

// Then
XCTAssertTrue(imageDownloadComplete, "image download complete should be true")
XCTAssertNotNil(imageView.image, "image view image should not be nil")
}

func testThatActiveRequestIsNilAfterImageDownloadCompletes() {
// Given
let expectation = expectationWithDescription("image should download successfully")
Expand Down

0 comments on commit bc0e4fb

Please sign in to comment.