Forráskód Böngészése

always ensure that only 1 timer is scheduled. Always set timer to nil, after being invalidated

cyberta 4 éve
szülő
commit
4ae44fea3b
1 módosított fájl, 13 hozzáadás és 13 törlés
  1. 13 13
      deltachat-ios/Chat/ChatViewController.swift

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

@@ -278,26 +278,26 @@ 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 {
+                guard let self = self, let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+                if appDelegate.appIsInForeground() {
+                    self.messageIds = self.getMessageIds()
+                    self.reloadData()
                 }
             }
         }
     }
 
     private func stopTimer() {
-        // check if the timer is not already stopped
-        if timer?.isValid ?? false {
-            timer?.invalidate()
+        if let timer = timer {
+            if timer.isValid {
+                timer.invalidate()
+            }
         }
+        timer = nil
     }
 
     private func configureEmptyStateView() {