Browse Source

move appDelegate check to main thread

B. Petersen 3 years ago
parent
commit
262084c4ac
1 changed files with 13 additions and 8 deletions
  1. 13 8
      deltachat-ios/Controller/ConnectivityViewController.swift

+ 13 - 8
deltachat-ios/Controller/ConnectivityViewController.swift

@@ -42,7 +42,7 @@ class ConnectivityViewController: WebViewViewController {
     }
 
     // this method needs to be run from a background thread
-    private func getNotificationStatus() -> String {
+    private func getNotificationStatus(hasNotifyToken: Bool) -> String {
         let title = " <b>" + String.localized("pref_notifications") + ":</b> "
         let notificationsEnabledInDC = !UserDefaults.standard.bool(forKey: "notifications_disabled")
         var notificationsEnabledInSystem = false
@@ -71,12 +71,10 @@ class ConnectivityViewController: WebViewViewController {
                 .appending(String.localized("notifications_disabled"))
         }
 
-        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
-            if appDelegate.notifyToken == nil {
-                return "<span class=\"red dot\"></span>"
-                    .appending(title)
-                    .appending("Service error")
-            }
+        if !hasNotifyToken {
+            return "<span class=\"red dot\"></span>"
+                .appending(title)
+                .appending("Service error")
         }
 
         let timestamps = UserDefaults.standard.array(forKey: Constants.Keys.notificationTimestamps) as? [Double]
@@ -134,6 +132,13 @@ class ConnectivityViewController: WebViewViewController {
     }
 
     private func loadHtml() {
+        // appDelegate needs to be called from main thread
+        var hasNotifyToken = false
+        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+            hasNotifyToken = appDelegate.notifyToken != nil
+        }
+
+        // do the remaining things in background thread
         DispatchQueue.global(qos: .userInitiated).async { [weak self] in
             guard let self = self else { return }
             var html = self.dcContext.getConnectivityHtml()
@@ -159,7 +164,7 @@ class ConnectivityViewController: WebViewViewController {
                     </style>
                     """)
 
-            let notificationStatus = self.getNotificationStatus()
+            let notificationStatus = self.getNotificationStatus(hasNotifyToken: hasNotifyToken)
             if let range = html.range(of: "</ul>") {
                 html = html.replacingCharacters(in: range, with: "<li>" + notificationStatus + "</li></ul>")
             }