Procházet zdrojové kódy

Only set session task delegate when starting download (#1115)

Only set session task delegate when starting download
Wei Wang před 7 roky
rodič
revize
2be32cbb55
5 změnil soubory, kde provedl 66 přidání a 59 odebrání
  1. 4 0
      .travis.yml
  2. 1 1
      Gemfile
  3. 19 19
      Gemfile.lock
  4. 38 38
      Sources/Networking/ImageDownloader.swift
  5. 4 1
      fastlane/Fastfile

+ 4 - 0
.travis.yml

@@ -3,6 +3,10 @@ osx_image: xcode10.1
 
 cache: bundler
 
+before_install:
+  - gem update --system
+  - gem install bundler
+
 matrix:
   include:
   - name: iOS | Swift 4.2

+ 1 - 1
Gemfile

@@ -4,4 +4,4 @@ source "https://rubygems.org"
 
 gem "fastlane"
 gem "jazzy"
-gem "cocoapods", "~> 1.6.0.beta"
+gem "cocoapods", "~> 1.6"

+ 19 - 19
Gemfile.lock

@@ -7,15 +7,15 @@ GEM
       minitest (~> 5.1)
       thread_safe (~> 0.3, >= 0.3.4)
       tzinfo (~> 1.1)
-    addressable (2.5.2)
+    addressable (2.6.0)
       public_suffix (>= 2.0.2, < 4.0)
     atomos (0.1.3)
     babosa (1.0.2)
     claide (1.0.2)
-    cocoapods (1.6.0.beta.2)
+    cocoapods (1.6.0)
       activesupport (>= 4.0.2, < 5)
       claide (>= 1.0.2, < 2.0)
-      cocoapods-core (= 1.6.0.beta.2)
+      cocoapods-core (= 1.6.0)
       cocoapods-deintegrate (>= 1.0.2, < 2.0)
       cocoapods-downloader (>= 1.2.2, < 2.0)
       cocoapods-plugins (>= 1.0.0, < 2.0)
@@ -25,13 +25,13 @@ GEM
       cocoapods-try (>= 1.1.0, < 2.0)
       colored2 (~> 3.1)
       escape (~> 0.0.4)
-      fourflusher (~> 2.0.1)
+      fourflusher (>= 2.2.0, < 3.0)
       gh_inspector (~> 1.0)
       molinillo (~> 0.6.6)
       nap (~> 1.0)
       ruby-macho (~> 1.3, >= 1.3.1)
-      xcodeproj (>= 1.7.0, < 2.0)
-    cocoapods-core (1.6.0.beta.2)
+      xcodeproj (>= 1.8.0, < 2.0)
+    cocoapods-core (1.6.0)
       activesupport (>= 4.0.2, < 6)
       fuzzy_match (~> 2.0.4)
       nap (~> 1.0)
@@ -40,7 +40,7 @@ GEM
     cocoapods-plugins (1.0.0)
       nap
     cocoapods-search (1.0.0)
-    cocoapods-stats (1.0.0)
+    cocoapods-stats (1.1.0)
     cocoapods-trunk (1.3.1)
       nap (>= 0.8, < 2.0)
       netrc (~> 0.11)
@@ -56,7 +56,7 @@ GEM
     domain_name (0.5.20180417)
       unf (>= 0.0.5, < 1.0.0)
     dotenv (2.6.0)
-    emoji_regex (0.1.1)
+    emoji_regex (1.0.1)
     escape (0.0.4)
     excon (0.62.0)
     faraday (0.15.4)
@@ -64,10 +64,10 @@ GEM
     faraday-cookie_jar (0.0.6)
       faraday (>= 0.7.4)
       http-cookie (~> 1.0.0)
-    faraday_middleware (0.12.2)
+    faraday_middleware (0.13.1)
       faraday (>= 0.7.4, < 1.0)
     fastimage (2.1.5)
-    fastlane (2.113.0)
+    fastlane (2.116.0)
       CFPropertyList (>= 2.3, < 4.0.0)
       addressable (>= 2.3, < 3.0.0)
       babosa (>= 1.0.2, < 2.0.0)
@@ -75,7 +75,7 @@ GEM
       colored
       commander-fastlane (>= 4.4.6, < 5.0.0)
       dotenv (>= 2.1.1, < 3.0.0)
-      emoji_regex (~> 0.1)
+      emoji_regex (>= 0.1, < 2.0)
       excon (>= 0.45.0, < 1.0.0)
       faraday (~> 0.9)
       faraday-cookie_jar (~> 0.0.6)
@@ -105,7 +105,7 @@ GEM
       xcpretty (~> 0.3.0)
       xcpretty-travis-formatter (>= 0.0.3)
     ffi (1.10.0)
-    fourflusher (2.0.1)
+    fourflusher (2.2.0)
     fuzzy_match (2.0.4)
     gh_inspector (1.1.3)
     google-api-client (0.23.9)
@@ -116,15 +116,15 @@ GEM
       representable (~> 3.0)
       retriable (>= 2.0, < 4.0)
       signet (~> 0.9)
-    google-cloud-core (1.2.7)
+    google-cloud-core (1.3.0)
       google-cloud-env (~> 1.0)
     google-cloud-env (1.0.5)
       faraday (~> 0.11)
-    google-cloud-storage (1.15.0)
+    google-cloud-storage (1.16.0)
       digest-crc (~> 0.4)
       google-api-client (~> 0.23)
       google-cloud-core (~> 1.2)
-      googleauth (~> 0.6.2)
+      googleauth (>= 0.6.2, < 0.10.0)
     googleauth (0.6.7)
       faraday (~> 0.12)
       jwt (>= 1.4, < 3.0)
@@ -196,7 +196,7 @@ GEM
       CFPropertyList
       naturally
     slack-notifier (2.3.2)
-    sqlite3 (1.3.13)
+    sqlite3 (1.4.0)
     terminal-notifier (1.8.0)
     terminal-table (1.8.0)
       unicode-display_width (~> 1.1, >= 1.1.1)
@@ -215,7 +215,7 @@ GEM
     word_wrap (1.0.0)
     xcinvoke (0.3.0)
       liferaft (~> 0.0.6)
-    xcodeproj (1.7.0)
+    xcodeproj (1.8.0)
       CFPropertyList (>= 2.3.3, < 4.0)
       atomos (~> 0.1.3)
       claide (>= 1.0.2, < 2.0)
@@ -230,9 +230,9 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
-  cocoapods (~> 1.6.0.beta)
+  cocoapods (~> 1.6)
   fastlane
   jazzy
 
 BUNDLED WITH
-   1.17.2
+   2.0.1

+ 38 - 38
Sources/Networking/ImageDownloader.swift

@@ -235,49 +235,49 @@ open class ImageDownloader {
         }
 
         let sessionTask = downloadTask.sessionTask
-        sessionTask.onTaskDone.delegate(on: self) { (self, done) in
-            // Underlying downloading finishes.
-            // result: Result<(Data, URLResponse?)>, callbacks: [TaskCallback]
-            let (result, callbacks) = done
 
-            // Before processing the downloaded data.
-            self.delegate?.imageDownloader(
-                self,
-                didFinishDownloadingImageForURL: url,
-                with: result.value?.1,
-                error: result.error)
-
-            switch result {
-            // Download finished. Now process the data to an image.
-            case .success(let (data, response)):
-                let processor = ImageDataProcessor(
-                    data: data, callbacks: callbacks, processingQueue: options.processingQueue)
-                processor.onImageProcessed.delegate(on: self) { (self, result) in
-                    // `onImageProcessed` will be called for `callbacks.count` times, with each
-                    // `SessionDataTask.TaskCallback` as the input parameter.
-                    // result: Result<Image>, callback: SessionDataTask.TaskCallback
-                    let (result, callback) = result
-
-                    if let image = result.value {
-                        self.delegate?.imageDownloader(self, didDownload: image, for: url, with: response)
+        // Start the session task if not started yet.
+        if !sessionTask.started {
+            sessionTask.onTaskDone.delegate(on: self) { (self, done) in
+                // Underlying downloading finishes.
+                // result: Result<(Data, URLResponse?)>, callbacks: [TaskCallback]
+                let (result, callbacks) = done
+
+                // Before processing the downloaded data.
+                self.delegate?.imageDownloader(
+                    self,
+                    didFinishDownloadingImageForURL: url,
+                    with: result.value?.1,
+                    error: result.error)
+
+                switch result {
+                // Download finished. Now process the data to an image.
+                case .success(let (data, response)):
+                    let processor = ImageDataProcessor(
+                        data: data, callbacks: callbacks, processingQueue: options.processingQueue)
+                    processor.onImageProcessed.delegate(on: self) { (self, result) in
+                        // `onImageProcessed` will be called for `callbacks.count` times, with each
+                        // `SessionDataTask.TaskCallback` as the input parameter.
+                        // result: Result<Image>, callback: SessionDataTask.TaskCallback
+                        let (result, callback) = result
+
+                        if let image = result.value {
+                            self.delegate?.imageDownloader(self, didDownload: image, for: url, with: response)
+                        }
+
+                        let imageResult = result.map { ImageLoadingResult(image: $0, url: url, originalData: data) }
+                        let queue = callback.options.callbackQueue
+                        queue.execute { callback.onCompleted?.call(imageResult) }
                     }
+                    processor.process()
 
-                    let imageResult = result.map { ImageLoadingResult(image: $0, url: url, originalData: data) }
-                    let queue = callback.options.callbackQueue
-                    queue.execute { callback.onCompleted?.call(imageResult) }
-                }
-                processor.process()
-
-            case .failure(let error):
-                callbacks.forEach { callback in
-                    let queue = callback.options.callbackQueue
-                    queue.execute { callback.onCompleted?.call(.failure(error)) }
+                case .failure(let error):
+                    callbacks.forEach { callback in
+                        let queue = callback.options.callbackQueue
+                        queue.execute { callback.onCompleted?.call(.failure(error)) }
+                    }
                 }
             }
-        }
-
-        // Start the session task if not started yet.
-        if !sessionTask.started {
             delegate?.imageDownloader(self, willDownloadImageForURL: url, with: request)
             sessionTask.resume()
         }

+ 4 - 1
fastlane/Fastfile

@@ -16,7 +16,10 @@ platform :ios do
   end
 
   lane :test do |options|
-    _test(options)
+    # Sometimes Travis CI would fail due to timeout. 
+    # Not sure why but just rerunning the test perfectly fixes it. :[
+    # Maybe it is related to some racing when running in CI, but it never reproducible in a local env.
+    _test(options) rescue _test(options)
   end
 
   private_lane :_test do |options|