From 072cbaf06c5d6d35d7ef795411f7acacfa75dc7d Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Sun, 10 Mar 2024 15:06:33 -0700 Subject: [PATCH] Fix VPN view model memory leak (#2570) Task/Issue URL: https://app.asana.com/0/414235014887631/1206803287661273/f Tech Design URL: CC: @graeme Description: This PR fixes a view model memory leak in the VPN code. --- DuckDuckGo/NetworkProtectionStatusViewModel.swift | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/DuckDuckGo/NetworkProtectionStatusViewModel.swift b/DuckDuckGo/NetworkProtectionStatusViewModel.swift index ab3ca1f57b..eb603f6162 100644 --- a/DuckDuckGo/NetworkProtectionStatusViewModel.swift +++ b/DuckDuckGo/NetworkProtectionStatusViewModel.swift @@ -37,7 +37,6 @@ final class NetworkProtectionStatusViewModel: ObservableObject { private let serverInfoObserver: ConnectionServerInfoObserver private let errorObserver: ConnectionErrorObserver private var cancellables: Set = [] - private var delayedToggleReenableCancellable: Cancellable? // MARK: Error @@ -158,12 +157,11 @@ final class NetworkProtectionStatusViewModel: ObservableObject { // Each event cancels the previous delayed publisher $shouldDisableToggle .filter { $0 } - .map { - Just(!$0) - .delay(for: 2.0, scheduler: DispatchQueue.main) - .assign(to: \.shouldDisableToggle, onWeaklyHeld: self) + .map { _ -> AnyPublisher in + Just(false).delay(for: 2.0, scheduler: DispatchQueue.main).eraseToAnyPublisher() } - .assign(to: \.delayedToggleReenableCancellable, onWeaklyHeld: self) + .switchToLatest() + .assign(to: \.shouldDisableToggle, onWeaklyHeld: self) .store(in: &cancellables) }