Prechádzať zdrojové kódy

stopFetch by duplicating code and not trying to be too smart

B. Petersen 4 rokov pred
rodič
commit
31ba6905a4
1 zmenil súbory, kde vykonal 14 pridanie a 9 odobranie
  1. 14 9
      deltachat-ios/AppDelegate.swift

+ 14 - 9
deltachat-ios/AppDelegate.swift

@@ -372,12 +372,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         bgIoTimestamp = nowTimestamp
 
         // make sure to balance each call to `beginBackgroundTask` with `endBackgroundTask`
-        let backgroundTask = UIApplication.shared.beginBackgroundTask {
-            // TODO: currently, this should be okay, we are not using too much background time,
-            // so that handler should never be called.
-            // the plan is to listen to an event as DC_EVENT_ENTER_IDLE and stop fetch from there.
-            // if we have such an event, we could imprive this handler and fire the event.
-            logger.info("fetch background task will end soon")
+        var backgroundTask: UIBackgroundTaskIdentifier = .invalid
+        backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
+            // usually, this handler is not used as we are taking care of timings below.
+            logger.info("⬅️ finishing fetch by system urgency requests")
+            self?.dcContext.stopIo()
+            completionHandler(.newData)
+            if backgroundTask != .invalid {
+                UIApplication.shared.endBackgroundTask(backgroundTask)
+                backgroundTask = .invalid
+            }
         }
 
         // we're in background, run IO for a little time
@@ -401,9 +405,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
                 logger.info("⬅️ fetch done")
                 completionHandler(.newData)
 
-                // this line should always be reached after a background task is started
-                // and balances the call to `beginBackgroundTask` above.
-                UIApplication.shared.endBackgroundTask(backgroundTask)
+                if backgroundTask != .invalid {
+                    UIApplication.shared.endBackgroundTask(backgroundTask)
+                    backgroundTask = .invalid
+                }
             }
         }
     }