From af821081b03dba96b283a9a8c992c80c79c14774 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 29 Feb 2020 17:36:54 -0500 Subject: [PATCH] Temporary fix for crash that occurs when switching accounts immediately after adding a new one --- .../PreferencesNavigationController.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Tusker/Screens/Preferences/PreferencesNavigationController.swift b/Tusker/Screens/Preferences/PreferencesNavigationController.swift index 3b4d7b4f..302eb832 100644 --- a/Tusker/Screens/Preferences/PreferencesNavigationController.swift +++ b/Tusker/Screens/Preferences/PreferencesNavigationController.swift @@ -57,8 +57,16 @@ class PreferencesNavigationController: UINavigationController { } @objc func activateAccount(_ notification: Notification) { + // TODO: this is a temporary measure + // when switching accounts shortly after adding a new one, there is an old instance of PreferncesNavigationController still around + // which tries to handle the notification but is unable to because it no longer is in a window (and therefore doesn't have a scene delegate) + // the propper fix would be to figure out what's leaking instances of this class + guard let window = self.view.window, + let windowScene = window.windowScene, + let sceneDelegate = windowScene.delegate as? SceneDelegate else { + return + } let account = notification.userInfo!["account"] as! LocalData.UserAccountInfo - let sceneDelegate = self.view.window!.windowScene!.delegate as! SceneDelegate isSwitchingAccounts = true dismiss(animated: true) { // dismiss preferences sceneDelegate.activateAccount(account) @@ -66,7 +74,11 @@ class PreferencesNavigationController: UINavigationController { } @objc func userLoggedOut() { - let sceneDelegate = self.view.window!.windowScene!.delegate as! SceneDelegate + guard let window = self.view.window, + let windowScene = window.windowScene, + let sceneDelegate = windowScene.delegate as? SceneDelegate else { + return + } isSwitchingAccounts = true dismiss(animated: true) { // dismiss preferences sceneDelegate.logoutCurrent()