Sfoglia il codice sorgente

adapt timer in ChatListController accordingly

cyberta 4 anni fa
parent
commit
57636c5dba
1 ha cambiato i file con 10 aggiunte e 5 eliminazioni
  1. 10 5
      deltachat-ios/Controller/ChatListController.swift

+ 10 - 5
deltachat-ios/Controller/ChatListController.swift

@@ -370,18 +370,23 @@ class ChatListController: UITableViewController {
     
     private func startTimer() {
         // check if the timer is not yet started
-        if !(timer?.isValid ?? false) {
-            timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
-                self?.refreshInBg()
+        stopTimer()
+        timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
+            guard let self = self, let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+            if appDelegate.appIsInForeground() {
+                self.refreshInBg()
+            } else {
+                logger.warning("startTimer() must not be executed in background")
             }
         }
     }
     
     private func stopTimer() {
         // check if the timer is not already stopped
-        if timer?.isValid ?? false {
-            timer?.invalidate()
+        if let timer = timer {
+            timer.invalidate()
         }
+        timer = nil
     }
 
     // MARK: - alerts