Improve Application lifetime scheduling

We have noticed race conditions between deep link handling and client
resuming/sync starting which lead to timelines being created without being able
to access the underlying stores.

This patch switches to foreground/background app tracking in an attempt at a cheap
way to fix the underlying issue. Arguably this was the correct way to do it to
being with.
This commit is contained in:
Stefan Ceriu
2026-05-22 15:26:31 +03:00
parent 5c4d0666a9
commit 97d4a35af7
@@ -1106,6 +1106,16 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
selector: #selector(applicationWillResignActive), selector: #selector(applicationWillResignActive),
name: UIApplication.willResignActiveNotification, name: UIApplication.willResignActiveNotification,
object: nil) object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive), selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification, name: UIApplication.didBecomeActiveNotification,
@@ -1128,15 +1138,21 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
@objc @objc
private func applicationWillTerminate() { private func applicationWillTerminate() {
MXLog.info("Application will terminate")
stopSync(isBackgroundTask: false) stopSync(isBackgroundTask: false)
} }
@objc
private func applicationDidEnterBackground() {
MXLog.info("Application did enter background")
scheduleDelayedSyncStop()
scheduleBackgroundAppRefresh()
}
@objc @objc
private func applicationWillResignActive() { private func applicationWillResignActive() {
MXLog.info("Application will resign active") MXLog.info("Application will resign active")
scheduleDelayedSyncStop()
scheduleBackgroundAppRefresh()
} }
private func scheduleDelayedSyncStop() { private func scheduleDelayedSyncStop() {
@@ -1157,12 +1173,17 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
} }
@objc @objc
private func applicationDidBecomeActive() { private func applicationWillEnterForeground() {
MXLog.info("Application did become active") MXLog.info("Application will enter foreground")
endActiveBackgroundTask() endActiveBackgroundTask()
startSync() startSync()
} }
@objc
private func applicationDidBecomeActive() {
MXLog.info("Application did become active")
}
private func endActiveBackgroundTask() { private func endActiveBackgroundTask() {
guard let backgroundTask else { guard let backgroundTask else {
return return