Browse Source

Merge pull request #1213 from deltachat/timer

Always set timer to nil after being invalidated
bjoern 4 năm trước cách đây
mục cha
commit
b0e88fa5f9

+ 16 - 13
deltachat-ios/Chat/ChatViewController.swift

@@ -278,26 +278,29 @@ class ChatViewController: 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
-                //reload table
-                DispatchQueue.main.async {
-                    guard let self = self, let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
-                    if appDelegate.appIsInForeground() {
-                        self.messageIds = self.getMessageIds()
-                        self.reloadData()
-                    }
+        stopTimer()
+        timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
+            // reload table
+            DispatchQueue.main.async { [weak self] in
+                guard let self = self,
+                      let appDelegate = UIApplication.shared.delegate as? AppDelegate
+                else { return }
+                
+                if appDelegate.appIsInForeground() {
+                    self.messageIds = self.getMessageIds()
+                    self.reloadData()
+                } 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
     }
 
     private func configureEmptyStateView() {

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

@@ -370,18 +370,27 @@ 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