Browse Source

Merge pull request #1210 from deltachat/hack-around-0xdead10cc

hack around 0xdead10cc
bjoern 4 years ago
parent
commit
d0e46add53
2 changed files with 16 additions and 5 deletions
  1. 11 2
      deltachat-ios/AppDelegate.swift
  2. 5 3
      deltachat-ios/Chat/ChatViewController.swift

+ 11 - 2
deltachat-ios/AppDelegate.swift

@@ -202,6 +202,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             } else if app.backgroundTimeRemaining < 10 {
                 logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), stopping")
                 self.dcContext.stopIo()
+
+                // to avoid 0xdead10cc exceptions, scheduled jobs need to be done before we get suspended;
+                // we increase the probabilty that this happens by waiting a moment before calling unregisterBackgroundTask()
                 DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                     logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), done")
                     self.unregisterBackgroundTask()
@@ -382,7 +385,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             if !self.appIsInForeground() {
                 self.dcContext.stopIo()
             }
-            completionHandler(.newData)
+
+            // to avoid 0xdead10cc exceptions, scheduled jobs need to be done before we get suspended;
+            // we increase the probabilty that this happens by waiting a moment before calling completionHandler()
+            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
+                logger.info("⬅️ fetch done")
+                completionHandler(.newData)
+            }
         }
     }
 
@@ -508,7 +517,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         dcContext.setStockTranslation(id: DC_STR_FORWARDED, localizationKey: "forwarded")
     }
 
-    private func appIsInForeground() -> Bool {
+    func appIsInForeground() -> Bool {
         switch UIApplication.shared.applicationState {
         case .background, .inactive:
             return false

+ 5 - 3
deltachat-ios/Chat/ChatViewController.swift

@@ -283,9 +283,11 @@ class ChatViewController: UITableViewController {
             timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
                 //reload table
                 DispatchQueue.main.async {
-                    guard let self = self else { return }
-                    self.messageIds = self.getMessageIds()
-                    self.reloadData()
+                    guard let self = self, let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+                    if appDelegate.appIsInForeground() {
+                        self.messageIds = self.getMessageIds()
+                        self.reloadData()
+                    }
                 }
             }
         }