Harden watch sync and App Group storage

This commit is contained in:
FuelBoard Contributor
2026-09-26 20:03:29 +01:00
parent 004fe2f787
commit c3a975eb10
5 changed files with 95 additions and 16 deletions
+16 -2
View File
@@ -11,6 +11,7 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
static let shared = WatchSyncManager()
private var pendingSnapshotPush = false
private var activationRequested = false
private override init() {
super.init()
@@ -18,19 +19,28 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
func activate() {
guard WCSession.isSupported() else { return }
pendingSnapshotPush = true
let session = WCSession.default
guard session.isWatchAppInstalled else { return }
pendingSnapshotPush = true
if session.delegate !== self {
session.delegate = self
}
guard session.activationState != .activated else { return }
guard !activationRequested else { return }
activationRequested = true
session.activate()
}
func pushSnapshot() {
guard WCSession.isSupported() else { return }
let session = WCSession.default
guard session.isWatchAppInstalled else {
pendingSnapshotPush = false
return
}
guard session.activationState == .activated else {
pendingSnapshotPush = true
activate()
return
}
do {
@@ -73,6 +83,9 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
}
nonisolated func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {
Task { @MainActor in
self.activationRequested = false
}
if let error {
print("WATCH-SYNC activation failed: \(error.localizedDescription)")
return
@@ -88,7 +101,8 @@ final class WatchSyncManager: NSObject, WCSessionDelegate {
nonisolated func sessionDidDeactivate(_ session: WCSession) {
Task { @MainActor in
WCSession.default.activate()
self.activationRequested = false
self.activate()
}
}