Bläddra i källkod

Merge pull request #50 from onevcat/fix/session-failed

Fix/session failed
Wei Wang 10 år sedan
förälder
incheckning
8dd8a1127d

+ 17 - 2
Kingfisher/ImageDownloader.swift

@@ -40,6 +40,20 @@ private let downloaderBarrierName = "com.onevcat.Kingfisher.ImageDownloader.Barr
 private let imageProcessQueueName = "com.onevcat.Kingfisher.ImageDownloader.Process."
 private let instance = ImageDownloader(name: defaultDownloaderName)
 
+
+/**
+The error code.
+
+- BadData: The downloaded data is not an image or the data is corrupted.
+- NotModified: The remote server responsed a 304 code. No image data downloaded.
+- InvalidURL: The URL is invalid.
+*/
+public enum KingfisherError: Int {
+    case BadData = 10000
+    case NotModified = 10001
+    case InvalidURL = 20000
+}
+
 /**
 *	Protocol of `ImageDownloader`.
 */
@@ -240,6 +254,9 @@ extension ImageDownloader: NSURLSessionDataDelegate {
     
     private func callbackWithImage(image: UIImage?, error: NSError?, imageURL: NSURL) {
         if let callbackPairs = self.fetchLoads[imageURL]?.callbacks {
+            
+            self.cleanForURL(imageURL)
+            
             for callbackPair in callbackPairs {
                 callbackPair.completionHander?(image: image, error: error, imageURL: imageURL)
             }
@@ -283,8 +300,6 @@ extension ImageDownloader: NSURLSessionDataDelegate {
                     } else {
                         self.callbackWithImage(nil, error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.BadData.rawValue, userInfo: nil), imageURL: URL)
                     }
-                    
-                    self.cleanForURL(URL)
                 })
             }
         }

+ 0 - 10
Kingfisher/KingfisherManager.swift

@@ -52,16 +52,6 @@ public class RetrieveImageTask {
 
 public let KingfisherErrorDomain = "com.onevcat.Kingfisher.Error"
 
-/**
-The error code.
-
-- BadData: The downloaded data is not an image or the data is corrupted.
-*/
-public enum KingfisherError: Int {
-    case BadData = 10000
-    case NotModified = 10001
-    case InvalidURL = 20000
-}
 
 private let instance = KingfisherManager()
 

+ 23 - 0
KingfisherTests/ImageDownloaderTests.swift

@@ -181,4 +181,27 @@ class ImageDownloaderTests: XCTestCase {
             LSNocilla.sharedInstance().start()
         }
     }
+    
+    func testDownloadResultErrorAndRetry() {
+        let expectation = expectationWithDescription("wait for downloading error")
+        
+        let URLString = testKeys[0]
+        stubRequest("GET", URLString).andFailWithError(NSError(domain: "stubError", code: -1, userInfo: nil))
+        let URL = NSURL(string: URLString)!
+        
+        downloader.downloadImageWithURL(URL, progressBlock: nil) { (image, error, imageURL) -> () in
+            XCTAssertNotNil(error, "Should return with an error")
+            
+            LSNocilla.sharedInstance().clearStubs()
+            stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
+            
+            // Retry the download
+            self.downloader.downloadImageWithURL(URL, progressBlock: nil, completionHandler: { (image, error, imageURL) -> () in
+                XCTAssertNil(error, "Download should be finished without error")
+                expectation.fulfill()
+            })
+        }
+        
+        waitForExpectationsWithTimeout(5, handler: nil)
+    }
 }

+ 6 - 0
circle.yml

@@ -0,0 +1,6 @@
+machine:
+  environment:
+    XCODE_SCHEME: Kingfisher
+  xcode:
+    version: "6.3.1"
+