Browse Source

Merge pull request #1764 from onevcat/ConfusedVorlon-async_fetch_data

Confused vorlon async fetch data
Wei Wang 4 years ago
parent
commit
5632aca2a5

+ 4 - 2
Sources/General/ImageSource/ImageDataProvider.swift

@@ -89,8 +89,10 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
     /// The key used in cache.
     public var cacheKey: String
 
-    public func data(handler: (Result<Data, Error>) -> Void) {
-        handler(Result(catching: { try Data(contentsOf: fileURL) }))
+    public func data(handler:@escaping (Result<Data, Error>) -> Void) {
+        DispatchQueue.global(qos: .userInitiated).async {
+            handler(Result(catching: { try Data(contentsOf: fileURL) }))
+        }
     }
 
     /// The URL of the local file on the disk.

+ 5 - 5
Tests/KingfisherTests/ImageDataProviderTests.swift

@@ -39,14 +39,14 @@ class ImageDataProviderTests: XCTestCase {
         XCTAssertEqual(provider.cacheKey, fileURL.absoluteString)
         XCTAssertEqual(provider.fileURL, fileURL)
         
-        var syncCalled = false
+        let exp = expectation(description: #function)
         provider.data { result in
             XCTAssertEqual(result.value, testImageData)
-            syncCalled = true
+            try! fm.removeItem(at: fileURL)
+            exp.fulfill()
         }
-        
-        XCTAssertTrue(syncCalled)
-        try! fm.removeItem(at: fileURL)
+
+        waitForExpectations(timeout: 1, handler: nil)
     }
     
     func testBase64ImageDataProvider() {