瀏覽代碼

background fetch: execute completion handler sequencially after maybe network was called

cyberta 4 年之前
父節點
當前提交
f3441ddd4b
共有 1 個文件被更改,包括 12 次插入16 次删除
  1. 12 16
      deltachat-ios/AppDelegate.swift

+ 12 - 16
deltachat-ios/AppDelegate.swift

@@ -368,27 +368,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         }
         bgIoTimestamp = nowTimestamp
 
-        // we're in background, run IO for a little time
-        dcContext.maybeStartIo()
-
         // maybeNetwork() shall not be called in ui thread;
-        // even if we're in backround now, app may be starting just now and maybeNetwork() will block in that case
-        // (series of performFetchWithCompletionHandler/applicationWillEnterForeground are not rare)
         DispatchQueue.global(qos: .background).async { [weak self] in
+            self?.dcContext.maybeStartIo()
             self?.dcContext.maybeNetwork()
-        }
+            
+            DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in
+                logger.info("⬅️ finishing fetch")
 
-        DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in
-            logger.info("⬅️ finishing fetch")
-
-            guard let self = self else {
-                completionHandler(.failed)
-                return
-            }
-            if !self.appIsInForeground() {
-                self.dcContext.stopIo()
+                guard let self = self else {
+                    completionHandler(.failed)
+                    return
+                }
+                if !self.appIsInForeground() {
+                    self.dcContext.stopIo()
+                }
+                completionHandler(.newData)
             }
-            completionHandler(.newData)
         }
     }