Harden watch sync and App Group storage
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user