From f6a7a20c60fea125f08f361f760d706b14f82e68 Mon Sep 17 00:00:00 2001 From: Denis Mullaraj Date: Fri, 25 Feb 2022 10:39:25 +0100 Subject: [PATCH] Invalidate timer when view goes away to fix memory leaking in a way to also keep supporting macOS 10.11 deployment target. --- .../Timer/TimerViewController/TimerViewController.swift | 4 +++- .../Timer/TimerViewController/TimerViewModel.swift | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewController.swift b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewController.swift index b15acbe89c..0900e2dd8d 100644 --- a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewController.swift +++ b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewController.swift @@ -114,7 +114,9 @@ class TimerViewController: NSViewController { override func viewDidDisappear() { super.viewDidDisappear() - + + viewModel.viewWillDisappear() + [projectAutocompleteDidResignObserver, tagsAutocompleteDidResignObserver] .compactMap { $0 } .forEach { NotificationCenter.default.removeObserver($0) } diff --git a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewModel.swift b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewModel.swift index 3e1816ced3..8fbfbd0961 100644 --- a/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewModel.swift +++ b/src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewModel.swift @@ -114,7 +114,7 @@ final class TimerViewModel: NSObject { var tagsDataSource = TagDataSource(items: TagStorage.shared.tags, updateNotificationName: .TagStorageChangedNotification) - private var timer: Timer! + private var timer: Timer? private var actionsUsedBeforeStart: Set = Set() @@ -138,10 +138,14 @@ final class TimerViewModel: NSObject { deinit { cancelNotificationObservers() - timer.invalidate() } // MARK: - Public + + func viewWillDisappear() { + timer?.invalidate() + timer = nil + } func startStopAction() { if timeEntry.isRunning() {