ソースを参照

tweak background fetch

B. Petersen 5 年 前
コミット
d2cb0691e4
2 ファイル変更15 行追加4 行削除
  1. 4 0
      DcCore/DcCore/DC/Wrapper.swift
  2. 11 4
      deltachat-ios/AppDelegate.swift

+ 4 - 0
DcCore/DcCore/DC/Wrapper.swift

@@ -209,6 +209,10 @@ public class DcContext {
         dc_stop_io(contextPointer)
     }
 
+    public func isIoRunning() -> Bool {
+        return dc_is_io_running(contextPointer) != 0
+    }
+
     public func setStockTranslation(id: Int32, localizationKey: String) {
         dc_set_stock_translation(contextPointer, UInt32(id), String.localized(localizationKey))
     }

+ 11 - 4
deltachat-ios/AppDelegate.swift

@@ -24,6 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     var reachability = Reachability()!
     var window: UIWindow?
     var state = ApplicationState.stopped
+    var appIsInForeground = false
 
     func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         // main()
@@ -96,19 +97,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         logger.info("---- background-fetch ----")
         startThreads()
 
-        // TODO-ASYNC: implement that correctly
-        sleep(15)
-        completionHandler(.newData)
+        // we have 30 seconds time for our job, leave some seconds for graceful termination.
+        // also, the faster we return, the sooner we get called again.
+        DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
+            if !self.appIsInForeground {
+                self.dcContext.stopIo()
+            }
+            completionHandler(.newData)
+        }
     }
 
     func applicationWillEnterForeground(_: UIApplication) {
         logger.info("---- foreground ----")
+        appIsInForeground = true
         startThreads()
     }
 
     func applicationDidEnterBackground(_: UIApplication) {
         logger.info("---- background ----")
-
+        appIsInForeground = false
         maybeStop()
     }