Fix background app refresh crashes. Make the user session an optional instead of it randomly crashing

This commit is contained in:
Stefan Ceriu
2023-05-17 17:37:16 +03:00
committed by Stefan Ceriu
parent ce36be4e8b
commit 3ed5e96e5d
@@ -29,7 +29,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private var backgroundTask: BackgroundTaskProtocol? private var backgroundTask: BackgroundTaskProtocol?
private var isSuspended = false private var isSuspended = false
private var userSession: UserSessionProtocol! { private var userSession: UserSessionProtocol? {
didSet { didSet {
userSessionObserver?.cancel() userSessionObserver?.cancel()
@@ -156,6 +156,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async { func handleInlineReply(_ service: NotificationManagerProtocol, content: UNNotificationContent, replyText: String) async {
guard let userSession else {
fatalError("User session not setup")
}
MXLog.info("[AppCoordinator] handle notification reply") MXLog.info("[AppCoordinator] handle notification reply")
guard let roomId = content.userInfo[NotificationConstants.UserInfoKey.roomIdentifier] as? String else { guard let roomId = content.userInfo[NotificationConstants.UserInfoKey.roomIdentifier] as? String else {
@@ -266,6 +270,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func startAuthenticationSoftLogout() { private func startAuthenticationSoftLogout() {
guard let userSession else {
fatalError("User session not setup")
}
Task { Task {
var displayName = "" var displayName = ""
if case .success(let name) = await userSession.clientProxy.loadUserDisplayName() { if case .success(let name) = await userSession.clientProxy.loadUserDisplayName() {
@@ -299,6 +307,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func setupUserSession() { private func setupUserSession() {
guard let userSession else {
fatalError("User session not setup")
}
let navigationSplitCoordinator = NavigationSplitCoordinator(placeholderCoordinator: SplashScreenCoordinator()) let navigationSplitCoordinator = NavigationSplitCoordinator(placeholderCoordinator: SplashScreenCoordinator())
let userSessionFlowCoordinator = UserSessionFlowCoordinator(userSession: userSession, let userSessionFlowCoordinator = UserSessionFlowCoordinator(userSession: userSession,
navigationSplitCoordinator: navigationSplitCoordinator, navigationSplitCoordinator: navigationSplitCoordinator,
@@ -326,6 +338,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func logout(isSoft: Bool) { private func logout(isSoft: Bool) {
guard let userSession else {
fatalError("User session not setup")
}
showLoadingIndicator() showLoadingIndicator()
defer { defer {
@@ -395,6 +411,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func observeUserSessionChanges() { private func observeUserSessionChanges() {
guard let userSession else {
fatalError("User session not setup")
}
userSessionObserver = userSession.callbacks userSessionObserver = userSession.callbacks
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] callback in .sink { [weak self] callback in
@@ -434,6 +454,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func clearCache() { private func clearCache() {
guard let userSession else {
fatalError("User session not setup")
}
showLoadingIndicator() showLoadingIndicator()
navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator()) navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator())
@@ -479,7 +503,10 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
} }
private func startSync() { private func startSync() {
userSession?.clientProxy.startSync() // We don't fatal error here because background app refreshes might be scheduled before the session is setup
guard let userSession else { return }
userSession.clientProxy.startSync()
let identifier = "StaleDataIndicator" let identifier = "StaleDataIndicator"