Bläddra i källkod

don't invoke installations from background queue

Morgan Chen 1 år sedan
förälder
incheckning
9870f38f75
1 ändrade filer med 22 tillägg och 18 borttagningar
  1. 22 18
      FirebaseSessions/Sources/Settings/SettingsDownloadClient.swift

+ 22 - 18
FirebaseSessions/Sources/Settings/SettingsDownloadClient.swift

@@ -51,27 +51,31 @@ class SettingsDownloader: SettingsDownloadClient {
       return
     }
 
-    installations.installationID { result in
-      switch result {
-      case let .success(installationsInfo):
-        let request = self.buildRequest(url: validURL, fiid: installationsInfo.0)
-        let task = URLSession.shared.dataTask(with: request) { data, response, error in
-          if let data {
-            if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
-              completion(.success(dict))
-            } else {
-              completion(.failure(
-                .JSONParseError("Failed to parse JSON to dictionary")
-              ))
+    DispatchQueue.main.async {
+      // Installation's FIRInstallationsIDController isn't thread-safe, so this has to go
+      // on the main thread.
+      self.installations.installationID { result in
+        switch result {
+        case let .success(installationsInfo):
+          let request = self.buildRequest(url: validURL, fiid: installationsInfo.0)
+          let task = URLSession.shared.dataTask(with: request) { data, response, error in
+            if let data {
+              if let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
+                completion(.success(dict))
+              } else {
+                completion(.failure(
+                  .JSONParseError("Failed to parse JSON to dictionary")
+                ))
+              }
+            } else if let error {
+              completion(.failure(.URLSessionError(error.localizedDescription)))
             }
-          } else if let error {
-            completion(.failure(.URLSessionError(error.localizedDescription)))
           }
+          // Start the task that sends the network request
+          task.resume()
+        case let .failure(error):
+          completion(.failure(.InstallationIDError(error.localizedDescription)))
         }
-        // Start the task that sends the network request
-        task.resume()
-      case let .failure(error):
-        completion(.failure(.InstallationIDError(error.localizedDescription)))
       }
     }
   }