diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 7dda81bdc..9f2b9e788 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -458,20 +458,21 @@ class AppCoordinator: AppCoordinatorProtocol { private func clearCache() { showLoadingIndicator() - defer { - hideLoadingIndicator() - } - navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator()) userSession.clientProxy.stopSync() userSessionFlowCoordinator?.stop() - userSessionStore.clearCacheFor(userSession: userSession) - + let userID = userSession.userID tearDownUserSession() - - stateMachine.processEvent(.startWithExistingSession) + + // Allow for everything to deallocate properly + Task { + try await Task.sleep(for: .seconds(2)) + userSessionStore.clearCache(for: userID) + stateMachine.processEvent(.startWithExistingSession) + hideLoadingIndicator() + } } } diff --git a/ElementX/Sources/Services/UserSession/UserSessionStore.swift b/ElementX/Sources/Services/UserSession/UserSessionStore.swift index eb5d6d1a1..df437a257 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionStore.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionStore.swift @@ -21,7 +21,7 @@ import MatrixRustSDK class UserSessionStore: UserSessionStoreProtocol { private let keychainController: KeychainControllerProtocol private let backgroundTaskService: BackgroundTaskServiceProtocol - private let cachesFolderName = "matrix-sdk-state" + private let matrixSDKStateKey = "matrix-sdk-state" /// Whether or not there are sessions in the store. var hasSessions: Bool { !keychainController.restorationTokens().isEmpty } @@ -91,9 +91,8 @@ class UserSessionStore: UserSessionStoreProtocol { deleteSessionDirectory(for: userID) } - func clearCacheFor(userSession: UserSessionProtocol) { - let userID = userSession.clientProxy.userID - deleteCachesFolder(for: userID) + func clearCache(for userID: String) { + deleteCaches(for: userID) } // MARK: - Private @@ -153,17 +152,17 @@ class UserSessionStore: UserSessionStoreProtocol { } } - private func deleteCachesFolder(for userID: String) { - let url = basePath(for: userID).appendingPathComponent(cachesFolderName) - + private func deleteCaches(for userID: String) { do { - try FileManager.default.removeItem(at: url) + for url in try FileManager.default.contentsOfDirectory(at: basePath(for: userID), includingPropertiesForKeys: nil) where url.path.contains(matrixSDKStateKey) { + try FileManager.default.removeItem(at: url) + } } catch { MXLog.failure("Failed deleting the session data: \(error)") } } - #warning("We should move this and the caches folder path to the rust side") + #warning("We should move this and the caches cleanup to the rust side") private func basePath(for userID: String) -> URL { // Rust sanitises the user ID replacing invalid characters with an _ let sanitisedUserID = userID.replacingOccurrences(of: ":", with: "_") diff --git a/ElementX/Sources/Services/UserSession/UserSessionStoreProtocol.swift b/ElementX/Sources/Services/UserSession/UserSessionStoreProtocol.swift index b07958ebc..15aed892a 100644 --- a/ElementX/Sources/Services/UserSession/UserSessionStoreProtocol.swift +++ b/ElementX/Sources/Services/UserSession/UserSessionStoreProtocol.swift @@ -47,5 +47,5 @@ protocol UserSessionStoreProtocol { func logout(userSession: UserSessionProtocol) /// Clears our all the matrix sdk state data for the specified session - func clearCacheFor(userSession: UserSessionProtocol) + func clearCache(for userID: String) }