Просмотр исходного кода

Add returning task for download method

onevcat 10 лет назад
Родитель
Сommit
5656a50635

+ 12 - 10
Kingfisher/ImageDownloader.swift

@@ -153,9 +153,9 @@ public extension ImageDownloader {
     */
     public func downloadImageWithURL(URL: NSURL,
                            progressBlock: ImageDownloaderProgressBlock?,
-                       completionHandler: ImageDownloaderCompletionHandler?)
+                       completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
     {
-        downloadImageWithURL(URL, options: KingfisherManager.DefaultOptions, progressBlock: progressBlock, completionHandler: completionHandler)
+        return downloadImageWithURL(URL, options: KingfisherManager.DefaultOptions, progressBlock: progressBlock, completionHandler: completionHandler)
     }
     
     /**
@@ -169,9 +169,9 @@ public extension ImageDownloader {
     public func downloadImageWithURL(URL: NSURL,
                                  options: KingfisherManager.Options,
                            progressBlock: ImageDownloaderProgressBlock?,
-                       completionHandler: ImageDownloaderCompletionHandler?)
+                       completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
     {
-        downloadImageWithURL(URL,
+        return downloadImageWithURL(URL,
             retrieveImageTask: nil,
                       options: options,
                 progressBlock: progressBlock,
@@ -182,10 +182,10 @@ public extension ImageDownloader {
                        retrieveImageTask: RetrieveImageTask?,
                                  options: KingfisherManager.Options,
                            progressBlock: ImageDownloaderProgressBlock?,
-                       completionHandler: ImageDownloaderCompletionHandler?)
+                       completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask?
     {
         if let retrieveImageTask = retrieveImageTask where retrieveImageTask.cancelled {
-            return
+            return retrieveImageTask.downloadTask
         }
         
         let timeout = self.downloadTimeout == 0.0 ? 15.0 : self.downloadTimeout
@@ -199,19 +199,21 @@ public extension ImageDownloader {
         // There is a possiblility that request modifier changed the url to `nil` or empty.
         if request.URL == nil || request.URL!.absoluteString.isEmpty {
             completionHandler?(image: nil, error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.InvalidURL.rawValue, userInfo: nil), imageURL: nil, originalData: nil)
-            return
+            return nil
         }
         
+        var task: RetrieveImageDownloadTask? = nil
         setupProgressBlock(progressBlock, completionHandler: completionHandler, forURL: request.URL!) {(session, fetchLoad) -> Void in
-            let task = session.dataTaskWithRequest(request)
-            task.priority = options.lowPriority ? NSURLSessionTaskPriorityLow : NSURLSessionTaskPriorityDefault
-            task.resume()
+            task = session.dataTaskWithRequest(request)
+            task!.priority = options.lowPriority ? NSURLSessionTaskPriorityLow : NSURLSessionTaskPriorityDefault
+            task!.resume()
             
             fetchLoad.shouldDecode = options.shouldDecode
             fetchLoad.scale = options.scale
             
             retrieveImageTask?.downloadTask = task
         }
+        return task
     }
     
     // A single key may have multiple callbacks. Only download once.

+ 1 - 1
Kingfisher/KingfisherManager.swift

@@ -45,7 +45,7 @@ public class RetrieveImageTask {
     */
     public func cancel() {
         // From Xcode 7 beta 6, the `dispatch_block_cancel` will crash at runtime.
-        // It fixed in Xcode 7.1.
+        // It is fixed in Xcode 7.1.
         // See https://github.com/onevcat/Kingfisher/issues/99 for more.
         if let diskRetrieveTask = diskRetrieveTask {
             dispatch_block_cancel(diskRetrieveTask)

+ 36 - 0
KingfisherTests/ImageDownloaderTests.swift

@@ -219,4 +219,40 @@ class ImageDownloaderTests: XCTestCase {
         }
         waitForExpectationsWithTimeout(5, handler: nil)
     }
+    
+    func testCancelDownloadTask() {
+        
+        let expectation = expectationWithDescription("wait for downloading")
+        
+        let URLString = testKeys[0]
+        stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
+        let URL = NSURL(string: URLString)!
+        
+        var progressBlockIsCalled = false
+        var completionBlockIsCalled = false
+        
+        let downloadTask = downloader.downloadImageWithURL(URL, progressBlock: { (receivedSize, totalSize) -> () in
+                progressBlockIsCalled = true
+            }) { (image, error, imageURL, originalData) -> () in
+                XCTAssertNotNil(error)
+                XCTAssertEqual(error!.code, -999)
+                completionBlockIsCalled = true
+        }
+        
+        XCTAssertNotNil(downloadTask)
+        downloadTask!.cancel()
+        
+        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.09)), dispatch_get_main_queue()) { () -> Void in
+            expectation.fulfill()
+            XCTAssert(progressBlockIsCalled == false, "ProgressBlock should not be called since it is canceled.")
+            XCTAssert(completionBlockIsCalled == true, "CompletionBlock should be called with error.")
+        }
+        
+        waitForExpectationsWithTimeout(5, handler: nil)
+    }
+    
+    func testDownloadTaskNil() {
+        let downloadTask = downloader.downloadImageWithURL(NSURL(string: "")!, progressBlock: nil, completionHandler: nil)
+        XCTAssertNil(downloadTask)
+    }
 }