From 47019b23af4550569584896b9bf97f7a14b82b4b Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 18:51:34 +0700 Subject: [PATCH 1/3] perf(sync): bring other devices' iCloud changes by CloudKit push on macOS and iOS --- .claude/rules/data-sync-storage.md | 1 + .../CloudKitSyncEngine.swift | 9 + .../TableProSyncTransport/SyncAdmission.swift | 4 +- .../SyncMetadataStorage.swift | 7 + .../SyncPushChannel.swift | 87 +++++ .../SyncPushRegistrar.swift | 13 + .../SyncPushSubscription.swift | 27 ++ .../SyncAdmissionTests.swift | 4 +- .../SyncMetadataStorageTests.swift | 10 + .../SyncPushChannelTests.swift | 166 ++++++++ .../SyncPushSubscriptionTests.swift | 60 +++ apple/TablePro/AppDelegate.swift | 15 + .../SyncCoordinator+DirtyMarking.swift | 75 ++++ .../Extensions/SyncCoordinator+Push.swift | 23 ++ .../TablePro/Core/Sync/SyncCoordinator.swift | 128 +++---- apple/TablePro/Core/Sync/SyncEchoGuard.swift | 10 + .../Core/Sync/SyncPushRegistrar+AppKit.swift | 9 + apple/TablePro/Core/Sync/SyncTransport.swift | 1 + apple/TablePro/TablePro.entitlements | 2 + .../TableProMobile/AppState.swift | 13 + .../TableProMobile/TableProMobile/Info.plist | 1 + .../Platform/SceneLifecycle.swift | 61 +++ .../Sync/IOSSyncCoordinator.swift | 21 +- .../Sync/IOSSyncTransport.swift | 1 + .../Sync/SyncPushRegistrar+UIKit.swift | 9 + .../TableProMobile/TableProMobileApp.swift | 11 +- .../TableProMobileRelease.entitlements | 2 + .../Mocks/RecordingPushRegistrar.swift | 19 + .../Mocks/RecordingSyncTransport.swift | 13 + .../Mocks/UnreachableSyncTransport.swift | 4 + .../Onboarding/AppStateFixture.swift | 3 +- .../SavedQueries/SavedQueryLibraryTests.swift | 3 +- .../Sync/AppStateSyncWiringTests.swift | 13 + .../Sync/IOSSyncActivationTests.swift | 3 +- .../Sync/IOSSyncCoordinatorTests.swift | 5 +- .../Sync/IOSSyncPushTests.swift | 158 ++++++++ .../Storage/AppSettingsValueColorsTests.swift | 3 +- .../Core/Sync/SyncCategoryRefetchTests.swift | 5 +- .../Core/Sync/SyncCoordinatorPushTests.swift | 353 ++++++++++++++++++ .../SyncCoordinatorSQLFavoritePullTests.swift | 2 +- .../Core/Sync/SyncCoordinatorThemeTests.swift | 6 +- .../Sync/SyncFullFetchDeletionTests.swift | 5 +- .../Sync/SyncSavedQueriesRefillTests.swift | 5 +- .../Helpers/SyncTestEnvironment.swift | 36 +- apple/scripts/ci/check-entitlements.py | 24 +- 45 files changed, 1312 insertions(+), 118 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushChannel.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushRegistrar.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushSubscription.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushChannelTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushSubscriptionTests.swift create mode 100644 apple/TablePro/Core/Sync/Extensions/SyncCoordinator+DirtyMarking.swift create mode 100644 apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Push.swift create mode 100644 apple/TablePro/Core/Sync/SyncPushRegistrar+AppKit.swift create mode 100644 apple/TableProMobile/TableProMobile/Sync/SyncPushRegistrar+UIKit.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Mocks/RecordingPushRegistrar.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Sync/IOSSyncPushTests.swift create mode 100644 apple/TableProTests/Core/Sync/SyncCoordinatorPushTests.swift diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index 1df20d4f16..d6809a939f 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -186,6 +186,7 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Whether plugin secure fields moved out of `connections.json` into the Keychain, set only when every field moved and the save succeeded | UserDefaults `com.TablePro.pluginSecureFieldsMovedToKeychain`, device-local, never synced | `ConnectionStorage` | | Whether a saved-queries store replaced a damaged file and waits for a full fetch, so push treats it as unreadable | Table `sync_refill` in the saved-queries SQLite store, dropped once a full fetch applied | `SQLFavoriteStorage` | | Deletions the server reported for a sync category that is off, applied when the category is next read | UserDefaults, device-local, never synced | `SyncMetadataStorage.deferredDeletions` | +| The iCloud account this device last saved the CloudKit change subscription for; cleared when sync turns on, on Upload Again, when the data is found removed, and when an activation brings a change no notice announced | UserDefaults `com.TablePro.sync.subscribedAccountId`, device-local, never synced | `SyncMetadataStorage.subscribedAccountId` through `SyncPushChannel` | | Where a connection came from (`origin`): this Mac, imported, iCloud | `connections.json`, never synced or exported | `ConnectionStorage` | | Table load timings (no identifying data, never uploaded) | JSON lines | `TableLoadHistoryStore` | | Reopen Last Session list (`LastSession.json`, plus `LastOpenConnections.json` holding only the ids that reconnect, which older builds read) | JSON | `LastOpenConnectionsStorage` | diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift index 7e5b4fd852..69df7386a3 100644 --- a/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/CloudKitSyncEngine.swift @@ -86,6 +86,15 @@ public actor CloudKitSyncEngine { Self.logger.trace("Created or confirmed sync zone: \(Self.zoneName)") } + // Every device saves the same id, so a save replaces the subscription rather than adding one. + public func saveSubscription() async throws { + guard let database else { throw Self.unavailable } + try await withRetry { + _ = try await database.save(SyncPushSubscription.make()) + } + Self.logger.info("Saved the CloudKit change subscription") + } + // MARK: - Push @discardableResult diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncAdmission.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncAdmission.swift index 3d681701fd..27082b71b9 100644 --- a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncAdmission.swift +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncAdmission.swift @@ -7,6 +7,8 @@ public enum SyncTrigger: Equatable, Sendable { case launch /// The app came forward, or the system woke it to refresh. case activation + /// CloudKit's silent notice that another device changed the private database. + case remoteChange /// Something synced was edited on this device. case localChange /// `CKAccountChanged`: the person signed in or out, or the account became ready. @@ -65,7 +67,7 @@ public enum SyncAdmission: Equatable, Sendable { return .full case .localChange: return isDue ? .full : .none - case .activation, .networkRestored: + case .activation, .remoteChange, .networkRestored: return isDue ? .full : .downloadOnly } case .signedOut, .accountNotReady, .accountRestricted: diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncMetadataStorage.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncMetadataStorage.swift index 546eb9078f..dcf9039e9b 100644 --- a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncMetadataStorage.swift +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncMetadataStorage.swift @@ -223,6 +223,12 @@ public final class SyncMetadataStorage: @unchecked Sendable { set { userDefaults.set(newValue, forKey: key("lastAccountId")) } } + // Device-local: every device saves the change subscription once for the account it syncs with. + public var subscribedAccountId: String? { + get { userDefaults.string(forKey: key("subscribedAccountId")) } + set { userDefaults.set(newValue, forKey: key("subscribedAccountId")) } + } + @discardableResult public func adoptAccount(_ accountId: String) -> SyncAccountChange { guard let recorded = lastAccountId else { @@ -281,6 +287,7 @@ public final class SyncMetadataStorage: @unchecked Sendable { zoneState = .unknown userDefaults.removeObject(forKey: key("lastSyncDate")) userDefaults.removeObject(forKey: key("lastAccountId")) + userDefaults.removeObject(forKey: key("subscribedAccountId")) for type in SyncRecordType.allCases { clearDirty(type: type) diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushChannel.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushChannel.swift new file mode 100644 index 0000000000..30773dcf8b --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushChannel.swift @@ -0,0 +1,87 @@ +import CloudKit +import Foundation +import os + +// Registration for CloudKit's change notices and the subscription that sends them. Nothing here runs while sync is off. +@MainActor +public final class SyncPushChannel { + private static let logger = Logger(subsystem: "com.TablePro", category: "SyncPush") + + private let registrar: SyncPushRegistrar + private let metadata: SyncMetadataStorage + private var isRequested = false + private var isRegistered = false + private var missedNotice = false + private var refusedAccount: String? + public private(set) var noticesReceived = 0 + + public init(registrar: SyncPushRegistrar, metadata: SyncMetadataStorage) { + self.registrar = registrar + self.metadata = metadata + } + + public func register() { + guard !isRequested else { return } + isRequested = true + registrar.register() + } + + // Only a registration this process asked for is taken back, so a launch with sync off calls nothing. + public func unregister() { + isRegistered = false + missedNotice = false + guard isRequested else { return } + isRequested = false + registrar.unregister() + } + + public func registrationDidFinish(registered: Bool) { + guard isRequested else { return } + isRegistered = registered + } + + public func noticeArrived() { + missedNotice = false + noticesReceived += 1 + } + + // While notices arrive, a download they did not start finds nothing new. One that does, with no notice since + // the last download began, means they stopped arriving. The subscription is saved again in case it is gone. + public func activationFoundUnannouncedChanges(noticesSince count: Int) -> Bool { + guard isLive, noticesReceived == count else { return false } + missedNotice = true + forgetSubscription() + return true + } + + public var isLive: Bool { + guard isRegistered, !missedNotice, let account = metadata.lastAccountId else { return false } + return metadata.subscribedAccountId == account + } + + // A failed save never fails the run. One CloudKit refuses is not sent again for that account until the next launch. + public func saveSubscriptionIfNeeded(_ save: () async throws -> Void) async { + guard let account = metadata.lastAccountId, + metadata.subscribedAccountId != account, + refusedAccount != account else { return } + do { + try await save() + metadata.subscribedAccountId = account + } catch { + Self.logger.error("Could not save the CloudKit change subscription: \(error.localizedDescription, privacy: .private)") + // Only a fault of the build or the app version stays until relaunch; anything else is tried on the next run. + switch SyncFailure(error) { + case .blocked(.unavailable), .blocked(.appUpdateRequired): + refusedAccount = account + default: + break + } + } + } + + // Turning sync on, or finding TablePro's data removed from iCloud, saves the subscription again in case it is gone. + public func forgetSubscription() { + metadata.subscribedAccountId = nil + refusedAccount = nil + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushRegistrar.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushRegistrar.swift new file mode 100644 index 0000000000..1e4f295552 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushRegistrar.swift @@ -0,0 +1,13 @@ +// Each app supplies its own: AppKit and UIKit answer a registration through the app delegate. +public struct SyncPushRegistrar: Sendable { + public let register: @MainActor @Sendable () -> Void + public let unregister: @MainActor @Sendable () -> Void + + public init( + register: @escaping @MainActor @Sendable () -> Void, + unregister: @escaping @MainActor @Sendable () -> Void + ) { + self.register = register + self.unregister = unregister + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushSubscription.swift b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushSubscription.swift new file mode 100644 index 0000000000..c998385588 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSyncTransport/SyncPushSubscription.swift @@ -0,0 +1,27 @@ +import CloudKit +import Foundation + +public enum SyncPushSubscription { + public static let id = "TableProSyncChanges" + + // A database subscription outlives a deleted or purged zone and announces the zone coming back. + public static func make() -> CKDatabaseSubscription { + let subscription = CKDatabaseSubscription(subscriptionID: id) + let notificationInfo = CKSubscription.NotificationInfo() + // Silent: no alert, badge or sound, so the system asks the person nothing. + notificationInfo.shouldSendContentAvailable = true + subscription.notificationInfo = notificationInfo + return subscription + } + + // The payload is only a signal: it starts the same download an activation does, and nothing in it is read. + public static func announcesChange( + _ userInfo: [AnyHashable: Any], + containerIdentifier: String = CloudKitSyncEngine.defaultContainerID + ) -> Bool { + guard let notification = CKNotification(fromRemoteNotificationDictionary: userInfo) as? CKDatabaseNotification else { + return false + } + return notification.subscriptionID == id && notification.containerIdentifier == containerIdentifier + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncAdmissionTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncAdmissionTests.swift index dff84ae622..1a7f1bad80 100644 --- a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncAdmissionTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncAdmissionTests.swift @@ -10,7 +10,7 @@ struct SyncAdmissionTests { private var earlier: Date { now.addingTimeInterval(-1) } private static let everyTrigger: [SyncTrigger] = [ - .launch, .activation, .localChange, .accountChange, .networkRestored, .scheduledRetry, .userRequest + .launch, .activation, .remoteChange, .localChange, .accountChange, .networkRestored, .scheduledRetry, .userRequest ] @Test("Nothing in the way admits every trigger", arguments: everyTrigger) @@ -27,6 +27,7 @@ struct SyncAdmissionTests { #expect(SyncAdmission.decide(for: .localChange, after: error, nextAttempt: later, now: now) == .none) #expect(SyncAdmission.decide(for: .activation, after: error, nextAttempt: later, now: now) == .downloadOnly) #expect(SyncAdmission.decide(for: .networkRestored, after: error, nextAttempt: later, now: now) == .downloadOnly) + #expect(SyncAdmission.decide(for: .remoteChange, after: error, nextAttempt: later, now: now) == .downloadOnly) } @Test("Full storage lets the person, the timer and an account change try the upload") @@ -83,6 +84,7 @@ struct SyncAdmissionTests { for error in [SyncError?.none, .blocked(.storageFull), .blocked(.dataDeletedFromICloud)] { #expect(SyncAdmission.decide(for: .activation, after: error, nextAttempt: nil, throttledUntil: later, now: now) == .none) #expect(SyncAdmission.decide(for: .scheduledRetry, after: error, nextAttempt: nil, throttledUntil: later, now: now) == .none) + #expect(SyncAdmission.decide(for: .remoteChange, after: error, nextAttempt: nil, throttledUntil: later, now: now) == .none) #expect(SyncAdmission.decide(for: .userRequest, after: error, nextAttempt: nil, throttledUntil: later, now: now) != .none) } #expect(SyncAdmission.decide(for: .activation, after: nil, nextAttempt: nil, throttledUntil: earlier, now: now) == .full) diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncMetadataStorageTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncMetadataStorageTests.swift index dfad98f70a..730bf05925 100644 --- a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncMetadataStorageTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncMetadataStorageTests.swift @@ -179,6 +179,16 @@ struct SyncMetadataStorageTests { #expect(storage.lastAccountId == "account") } + @Test("The subscribed account round-trips and a full reset forgets it") + func subscribedAccountIdRoundTrips() { + let storage = makeStorage() + #expect(storage.subscribedAccountId == nil) + storage.subscribedAccountId = "account" + #expect(storage.subscribedAccountId == "account") + storage.clearAll() + #expect(storage.subscribedAccountId == nil) + } + @Test("The first account seen on a device that never synced is recorded and nothing queued is dropped") func firstAccountIsRecorded() { let defaults = ScratchDefaults.make() ?? .standard diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushChannelTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushChannelTests.swift new file mode 100644 index 0000000000..897cb195f4 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushChannelTests.swift @@ -0,0 +1,166 @@ +import CloudKit +import Foundation +import Testing + +import TableProSyncTransport + +@MainActor +struct SyncPushChannelTests { + private let metadata: SyncMetadataStorage + private let calls = RegistrarCalls() + + init() { + metadata = SyncMetadataStorage(userDefaults: ScratchDefaults.make() ?? .standard) + metadata.lastAccountId = "account-a" + } + + private func makeChannel() -> SyncPushChannel { + let calls = calls + return SyncPushChannel( + registrar: SyncPushRegistrar(register: { calls.registrations += 1 }, unregister: { calls.unregistrations += 1 }), + metadata: metadata + ) + } + + @Test("Registering twice asks once, and nothing is taken back that was never asked for") + func registrationIsAskedOnce() { + let channel = makeChannel() + channel.unregister() + #expect(calls.unregistrations == 0) + + channel.register() + channel.register() + #expect(calls.registrations == 1) + + channel.unregister() + #expect(calls.unregistrations == 1) + } + + @Test("Push is live only with a device registration and this account's subscription") + func liveNeedsRegistrationAndSubscription() async { + let channel = makeChannel() + channel.registrationDidFinish(registered: true) + #expect(!channel.isLive) + + channel.register() + channel.registrationDidFinish(registered: true) + #expect(!channel.isLive) + + await channel.saveSubscriptionIfNeeded {} + #expect(channel.isLive) + + metadata.lastAccountId = "account-b" + #expect(!channel.isLive) + } + + @Test("A failed registration or turning sync off ends push") + func failedRegistrationEndsPush() async { + let channel = makeChannel() + channel.register() + await channel.saveSubscriptionIfNeeded {} + channel.registrationDidFinish(registered: false) + #expect(!channel.isLive) + + channel.registrationDidFinish(registered: true) + #expect(channel.isLive) + channel.unregister() + #expect(!channel.isLive) + } + + @Test("Unannounced changes end push and save the subscription again, until the next notice") + func unannouncedChangesEndPush() async { + let channel = makeChannel() + #expect(!channel.activationFoundUnannouncedChanges(noticesSince: channel.noticesReceived)) + + channel.register() + channel.registrationDidFinish(registered: true) + await channel.saveSubscriptionIfNeeded {} + #expect(channel.activationFoundUnannouncedChanges(noticesSince: channel.noticesReceived)) + #expect(!channel.isLive) + #expect(metadata.subscribedAccountId == nil) + + var saves = 0 + await channel.saveSubscriptionIfNeeded { saves += 1 } + #expect(saves == 1) + #expect(!channel.isLive) + + channel.noticeArrived() + #expect(channel.isLive) + } + + @Test("Changes an activation brings are announced when a notice came since the last download began") + func noticeSinceTheLastDownloadAnnouncesChanges() async { + let channel = makeChannel() + channel.register() + channel.registrationDidFinish(registered: true) + await channel.saveSubscriptionIfNeeded {} + let atLastDownload = channel.noticesReceived + + channel.noticeArrived() + + #expect(!channel.activationFoundUnannouncedChanges(noticesSince: atLastDownload)) + #expect(channel.isLive) + } + + @Test("The subscription is saved once per account") + func savedOncePerAccount() async { + let channel = makeChannel() + var saves = 0 + await channel.saveSubscriptionIfNeeded { saves += 1 } + await channel.saveSubscriptionIfNeeded { saves += 1 } + #expect(saves == 1) + #expect(metadata.subscribedAccountId == "account-a") + + metadata.lastAccountId = "account-b" + await channel.saveSubscriptionIfNeeded { saves += 1 } + #expect(saves == 2) + #expect(metadata.subscribedAccountId == "account-b") + + channel.forgetSubscription() + await channel.saveSubscriptionIfNeeded { saves += 1 } + #expect(saves == 3) + } + + @Test("A save that fails for a reason that can pass is tried again on the next run", + arguments: [CKError.Code.networkFailure, .accountTemporarilyUnavailable, .internalError, .serviceUnavailable]) + func passingFailureRetries(_ code: CKError.Code) async { + let channel = makeChannel() + var saves = 0 + await channel.saveSubscriptionIfNeeded { + saves += 1 + throw CKError(code) + } + #expect(metadata.subscribedAccountId == nil) + + await channel.saveSubscriptionIfNeeded { saves += 1 } + #expect(saves == 2) + #expect(metadata.subscribedAccountId == "account-a") + } + + @Test("A save CloudKit refuses is not sent again for that account, but is for the next one") + func refusedSaveIsNotRepeated() async { + let channel = makeChannel() + var saves = 0 + let refuse: () async throws -> Void = { + saves += 1 + throw CKError(.permissionFailure) + } + await channel.saveSubscriptionIfNeeded(refuse) + await channel.saveSubscriptionIfNeeded(refuse) + #expect(saves == 1) + + metadata.lastAccountId = "account-b" + await channel.saveSubscriptionIfNeeded(refuse) + #expect(saves == 2) + + channel.forgetSubscription() + await channel.saveSubscriptionIfNeeded(refuse) + #expect(saves == 3) + } +} + +@MainActor +private final class RegistrarCalls { + var registrations = 0 + var unregistrations = 0 +} diff --git a/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushSubscriptionTests.swift b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushSubscriptionTests.swift new file mode 100644 index 0000000000..2d5250eaf7 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSyncTests/SyncPushSubscriptionTests.swift @@ -0,0 +1,60 @@ +import CloudKit +import Foundation +import Testing + +import TableProSyncTransport + +struct SyncPushSubscriptionTests { + // The shape CloudKit sends for a database subscription, read back by CloudKit's own parser. + static func payload( + subscriptionID: String = SyncPushSubscription.id, + container: String = CloudKitSyncEngine.defaultContainerID + ) -> [AnyHashable: Any] { + [ + "aps": ["content-available": 1], + "ck": [ + "ce": 2, + "cid": container, + "nid": UUID().uuidString, + "met": ["dbs": 1, "sid": subscriptionID] + ] as [String: Any] + ] + } + + @Test("The subscription covers the whole private database and is silent") + func subscriptionIsSilent() throws { + let subscription = SyncPushSubscription.make() + #expect(subscription.subscriptionID == SyncPushSubscription.id) + #expect(subscription.recordType == nil) + let info = try #require(subscription.notificationInfo) + #expect(info.shouldSendContentAvailable) + #expect(info.alertBody == nil) + #expect(info.alertLocalizationKey == nil) + #expect(info.title == nil) + #expect(info.soundName == nil) + #expect(!info.shouldBadge) + } + + @Test("A notice from the subscription announces a change") + func ownNoticeAnnouncesChange() { + #expect(SyncPushSubscription.announcesChange(Self.payload())) + } + + @Test("Another subscription, another container or another kind of push announces nothing") + func otherPushesAnnounceNothing() { + #expect(!SyncPushSubscription.announcesChange(Self.payload(subscriptionID: "other"))) + #expect(!SyncPushSubscription.announcesChange(Self.payload(container: "iCloud.com.example"))) + #expect(!SyncPushSubscription.announcesChange(["aps": ["content-available": 1]])) + #expect(!SyncPushSubscription.announcesChange(["aps": ["alert": "Hello"]])) + let zoneNotice: [AnyHashable: Any] = [ + "aps": ["content-available": 1], + "ck": [ + "ce": 2, + "cid": CloudKitSyncEngine.defaultContainerID, + "nid": UUID().uuidString, + "fet": ["dbs": 1, "sid": SyncPushSubscription.id, "zid": CloudKitSyncEngine.zoneName, "zoid": "_owner"] + ] as [String: Any] + ] + #expect(!SyncPushSubscription.announcesChange(zoneNotice)) + } +} diff --git a/apple/TablePro/AppDelegate.swift b/apple/TablePro/AppDelegate.swift index ee214d45f4..b15592456e 100644 --- a/apple/TablePro/AppDelegate.swift +++ b/apple/TablePro/AppDelegate.swift @@ -170,6 +170,21 @@ class AppDelegate: NSObject, NSApplicationDelegate { true } + // MARK: - Remote Notifications + + func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { + SyncCoordinator.shared.pushRegistrationDidFinish(registered: true) + } + + func application(_ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: any Error) { + Self.logger.warning("Push registration failed: \(error.localizedDescription, privacy: .private)") + SyncCoordinator.shared.pushRegistrationDidFinish(registered: false) + } + + func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String: Any]) { + SyncCoordinator.shared.handleRemoteNotification(userInfo) + } + /// Unhiding is the one way a window comes back without any window notification firing, and the /// app reports every window it owns as invisible while it is hidden. func applicationDidUnhide(_ notification: Notification) { diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+DirtyMarking.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+DirtyMarking.swift new file mode 100644 index 0000000000..e9b9580b57 --- /dev/null +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+DirtyMarking.swift @@ -0,0 +1,75 @@ +import Foundation +import os +import TableProSyncTransport + +extension SyncCoordinator { + /// Marks existing SQL favorites and folders dirty. Separate from `markAllLocalDataDirty` + /// because the favorite store is an actor and must be read asynchronously. + func markSQLFavoritesDirty() async { + let favorites = await services.sqlFavoriteManager.fetchFavorites() + changeTracker.markDirty(.favorite, ids: favorites.map { $0.id.uuidString }) + + let folders = await services.sqlFavoriteManager.fetchFolders() + changeTracker.markDirty(.favoriteFolder, ids: folders.map { $0.id.uuidString }) + } + + /// Marks every synced record dirty so the first sync after enabling pushes the lot. + /// + /// Every type is marked as one batch. Marking record by record posted a change notification per + /// record, and an account with a few hundred saved column layouts built a chain of hundreds of + /// tasks each waiting on its predecessor until the app stopped responding to the switch that + /// started it. + func markAllLocalDataDirty() { + let connections = services.connectionStorage.loadConnections() + changeTracker.markDirty( + .connection, + ids: connections.filter(\.participatesInSync).map { $0.id.uuidString } + ) + + let groups = services.groupStorage.loadGroups() + changeTracker.markDirty(.group, ids: groups.map { $0.id.uuidString }) + + let tags = services.tagStorage.loadTags() + changeTracker.markDirty(.tag, ids: tags.map { $0.id.uuidString }) + + let sshProfiles = services.sshProfileStorage.loadProfiles() + changeTracker.markDirty(.sshProfile, ids: sshProfiles.map { $0.id.uuidString }) + + let credentialProfiles = services.credentialProfileStorage.loadProfiles() + changeTracker.markDirty(.credentialProfile, ids: credentialProfiles.map { $0.id.uuidString }) + + let favoriteTables = services.favoriteTablesStorage.loadFavorites() + changeTracker.markDirty( + .tableFavorite, + ids: favoriteTables.map { FavoriteTablesStorage.syncId(for: $0) } + ) + + let favoriteDatabases = services.favoriteDatabasesStorage.loadFavorites() + changeTracker.markDirty( + .favoriteDatabase, + ids: favoriteDatabases.map { FavoriteDatabasesStorage.syncId(for: $0) } + ) + + let tableFolderIds = tableFolderSyncIds() + changeTracker.markDirty(.tableFolder, ids: tableFolderIds.folders) + changeTracker.markDirty(.tableFolderItem, ids: tableFolderIds.items) + + let settingsCategories = AppSettingsCategory.synced + [CustomSlashCommandStorage.syncCategory] + let columnLayoutCategories = columnLayouts().customizedStorageKeys() + .map { FileColumnLayoutPersister.syncCategory(for: $0) } + let themeCategories = themeCatalog.syncCategories + changeTracker.markDirty(.settings, ids: settingsCategories + columnLayoutCategories + themeCategories) + + let summary = [ + "connections=\(connections.count)", + "groups=\(groups.count)", + "tags=\(tags.count)", + "sshProfiles=\(sshProfiles.count)", + "favoriteTables=\(favoriteTables.count)", + "tableFolders=\(tableFolderIds.folders.count)", + "settings=\(AppSettingsCategory.synced.count + 1)", + "themes=\(themeCategories.count)" + ].joined(separator: ", ") + Self.logger.info("Marked all local data dirty: \(summary, privacy: .public)") + } +} diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Push.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Push.swift new file mode 100644 index 0000000000..ce893efdb7 --- /dev/null +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Push.swift @@ -0,0 +1,23 @@ +import Foundation +import TableProSyncTransport + +extension SyncCoordinator { + // CloudKit registers the device for its own notices, so the token goes nowhere. + func pushRegistrationDidFinish(registered: Bool) { + pushChannel.registrationDidFinish(registered: registered) + } + + func handleRemoteNotification(_ userInfo: [AnyHashable: Any]) { + guard syncStatus.isEnabled, SyncPushSubscription.announcesChange(userInfo) else { return } + pushChannel.noticeArrived() + requestSync(.remoteChange) + } + + var isPushLive: Bool { + pushChannel.isLive + } + + var activationHoldInterval: Duration { + isPushLive ? Self.pushFallbackInterval : Self.activationInterval + } +} diff --git a/apple/TablePro/Core/Sync/SyncCoordinator.swift b/apple/TablePro/Core/Sync/SyncCoordinator.swift index 389ee50e76..5db2c92cf3 100644 --- a/apple/TablePro/Core/Sync/SyncCoordinator.swift +++ b/apple/TablePro/Core/Sync/SyncCoordinator.swift @@ -69,6 +69,12 @@ final class SyncCoordinator: ObservableObject { private var activationHeldForCooldown = false private var downloadIsAheadInRun = false + // While CloudKit's notices reach this Mac they bring other devices' changes, so an activation only covers a lost one. + nonisolated static let pushFallbackInterval: Duration = .seconds(15 * 60) + let pushChannel: SyncPushChannel + // Notices received when the last successful download began; one after that may be what a later download brings. + private var noticesAtLastDownload = 0 + init( services: AppServices = .live, recordCache: SyncRecordCache = SyncCoordinator.makeRecordCache(), @@ -78,10 +84,12 @@ final class SyncCoordinator: ObservableObject { themeCatalog: ThemeCatalog = .shared, licenseGate: (@MainActor () -> DisableReason?)? = nil, recordCreationDate: @escaping (CKRecord) -> Date? = { $0.creationDate }, - activationClock: any Clock = ContinuousClock() + activationClock: any Clock = ContinuousClock(), + pushRegistrar: SyncPushRegistrar = .live ) { self.services = services self.activationClock = activationClock + self.pushChannel = SyncPushChannel(registrar: pushRegistrar, metadata: services.syncMetadataStorage) self.makeTransport = transport self.makeNetworkMonitor = makeNetworkMonitor self.recordCreationDate = recordCreationDate @@ -149,6 +157,10 @@ final class SyncCoordinator: ObservableObject { if trigger == .activation, holdsActivationBack() { return } + // The download still ahead in this run brings what the notice announced. + if trigger == .remoteChange, isRunning, downloadIsAheadInRun { + return + } guard !isRunning else { if !pendingTriggers.contains(trigger) { pendingTriggers.append(trigger) @@ -176,7 +188,7 @@ final class SyncCoordinator: ObservableObject { if previousError == nil || trigger == .userRequest { syncStatus = .syncing } - let result = await accountThenPushThenPull(admission) + let result = await accountThenPushThenPull(admission, startedBy: trigger) isRunning = false finish(result, previousError: previousError, from: generation) if uploadAgainRequested { @@ -205,9 +217,10 @@ final class SyncCoordinator: ObservableObject { activationCooldown?.cancel() activationHeldForCooldown = false let clock = activationClock + let interval = activationHoldInterval activationCooldown = Task { [weak self] in do { - try await clock.sleep(for: Self.activationInterval) + try await clock.sleep(for: interval) } catch { return } @@ -221,7 +234,7 @@ final class SyncCoordinator: ObservableObject { } internal func runSyncCycle(_ admission: SyncAdmission = .full) async -> SyncError? { - await accountThenPushThenPull(admission).failure?.error + await accountThenPushThenPull(admission, startedBy: .userRequest).failure?.error } private struct CycleResult { @@ -229,7 +242,7 @@ final class SyncCoordinator: ObservableObject { var failure: SyncStepFailure? } - private func accountThenPushThenPull(_ requested: SyncAdmission) async -> CycleResult { + private func accountThenPushThenPull(_ requested: SyncAdmission, startedBy trigger: SyncTrigger) async -> CycleResult { var admission = requested downloadIsAheadInRun = true defer { downloadIsAheadInRun = false } @@ -242,6 +255,7 @@ final class SyncCoordinator: ObservableObject { admission = .full } } + await pushChannel.saveSubscriptionIfNeeded { try await transport.saveSubscription() } /// Only a run that may upload creates the zone. A download looks for it, and finding it /// again is how this Mac learns another device brought deleted data back. @@ -256,8 +270,15 @@ final class SyncCoordinator: ObservableObject { } let push = admission == .full ? await performPush() : PushReport() - let pullFailure = await performPull(echoGuard: push.echoGuard) + let noticesAtThisDownload = pushChannel.noticesReceived + let pull = await performPull(echoGuard: push.echoGuard) + let pullFailure = pull.failure if pullFailure == nil { + if trigger == .activation, pull.broughtRemoteChanges, + pushChannel.activationFoundUnannouncedChanges(noticesSince: noticesAtLastDownload) { + Self.logger.notice("An activation brought changes no CloudKit notice announced, so activations download once a minute again") + } + noticesAtLastDownload = noticesAtThisDownload startActivationCooldown() } @@ -274,6 +295,7 @@ final class SyncCoordinator: ObservableObject { recordCache.removeAll() metadataStorage.deferredDeletions = [] metadataStorage.saveToken(nil) + pushChannel.forgetSubscription() } metadataStorage.zoneState = nextZone return CycleResult(admission: admission, failure: failure) @@ -435,6 +457,7 @@ final class SyncCoordinator: ObservableObject { // Clear token to force a full fetch on first sync after enabling metadataStorage.saveToken(nil) + pushChannel.forgetSubscription() metadataStorage.deferredDeletions = [] /// Turning sync on is the person's choice to upload, a removed zone included. metadataStorage.zoneState = .unknown @@ -463,6 +486,7 @@ final class SyncCoordinator: ObservableObject { metadataStorage.zoneState = .unknown metadataStorage.saveToken(nil) metadataStorage.deferredDeletions = [] + pushChannel.forgetSubscription() recordCache.removeAll() markAllLocalDataDirty() await markSQLFavoritesDirty() @@ -473,76 +497,6 @@ final class SyncCoordinator: ObservableObject { await sync(.userRequest) } - /// Marks existing SQL favorites and folders dirty. Separate from `markAllLocalDataDirty` - /// because the favorite store is an actor and must be read asynchronously. - private func markSQLFavoritesDirty() async { - let favorites = await services.sqlFavoriteManager.fetchFavorites() - changeTracker.markDirty(.favorite, ids: favorites.map { $0.id.uuidString }) - - let folders = await services.sqlFavoriteManager.fetchFolders() - changeTracker.markDirty(.favoriteFolder, ids: folders.map { $0.id.uuidString }) - } - - /// Marks every synced record dirty so the first sync after enabling pushes the lot. - /// - /// Every type is marked as one batch. Marking record by record posted a change notification per - /// record, and an account with a few hundred saved column layouts built a chain of hundreds of - /// tasks each waiting on its predecessor until the app stopped responding to the switch that - /// started it. - private func markAllLocalDataDirty() { - let connections = services.connectionStorage.loadConnections() - changeTracker.markDirty( - .connection, - ids: connections.filter(\.participatesInSync).map { $0.id.uuidString } - ) - - let groups = services.groupStorage.loadGroups() - changeTracker.markDirty(.group, ids: groups.map { $0.id.uuidString }) - - let tags = services.tagStorage.loadTags() - changeTracker.markDirty(.tag, ids: tags.map { $0.id.uuidString }) - - let sshProfiles = services.sshProfileStorage.loadProfiles() - changeTracker.markDirty(.sshProfile, ids: sshProfiles.map { $0.id.uuidString }) - - let credentialProfiles = services.credentialProfileStorage.loadProfiles() - changeTracker.markDirty(.credentialProfile, ids: credentialProfiles.map { $0.id.uuidString }) - - let favoriteTables = services.favoriteTablesStorage.loadFavorites() - changeTracker.markDirty( - .tableFavorite, - ids: favoriteTables.map { FavoriteTablesStorage.syncId(for: $0) } - ) - - let favoriteDatabases = services.favoriteDatabasesStorage.loadFavorites() - changeTracker.markDirty( - .favoriteDatabase, - ids: favoriteDatabases.map { FavoriteDatabasesStorage.syncId(for: $0) } - ) - - let tableFolderIds = tableFolderSyncIds() - changeTracker.markDirty(.tableFolder, ids: tableFolderIds.folders) - changeTracker.markDirty(.tableFolderItem, ids: tableFolderIds.items) - - let settingsCategories = AppSettingsCategory.synced + [CustomSlashCommandStorage.syncCategory] - let columnLayoutCategories = columnLayouts().customizedStorageKeys() - .map { FileColumnLayoutPersister.syncCategory(for: $0) } - let themeCategories = themeCatalog.syncCategories - changeTracker.markDirty(.settings, ids: settingsCategories + columnLayoutCategories + themeCategories) - - let summary = [ - "connections=\(connections.count)", - "groups=\(groups.count)", - "tags=\(tags.count)", - "sshProfiles=\(sshProfiles.count)", - "favoriteTables=\(favoriteTables.count)", - "tableFolders=\(tableFolderIds.folders.count)", - "settings=\(AppSettingsCategory.synced.count + 1)", - "themes=\(themeCategories.count)" - ].joined(separator: ", ") - Self.logger.info("Marked all local data dirty: \(summary, privacy: .public)") - } - /// Called when user disables sync in settings func disableSync() { decide(.disabled(.userDisabled)) @@ -583,6 +537,7 @@ final class SyncCoordinator: ObservableObject { arm() return } + pushChannel.unregister() triggers = nil resetRetryState() pendingTriggers = [] @@ -740,9 +695,15 @@ final class SyncCoordinator: ObservableObject { Self.logger.info("Reading record types the last pull skipped: cleared sync token for full fetch") } + private struct PullReport { + var failure: SyncStepFailure? + // A delta that carried records this run did not upload, which another device sent. + var broughtRemoteChanges = false + } + /// Reports a failed download instead of passing over it: a run whose pull failed did not bring /// this Mac up to date, and must not say Synced or move Last Synced. - private func performPull(echoGuard: SyncEchoGuard?) async -> SyncStepFailure? { + private func performPull(echoGuard: SyncEchoGuard?) async -> PullReport { // Applied with the categories recorded here, so one turned off during the download is still read once. let settings = services.appSettingsStorage.loadSync() adoptReadableContent(for: settings) @@ -754,22 +715,24 @@ final class SyncCoordinator: ObservableObject { do { let result = try await transport.pull(since: token) - return await applyPull(result, isFullFetch: token == nil, settings: settings, echoGuard: echoGuard) - ? nil : .pullNotSaved + guard await applyPull(result, isFullFetch: token == nil, settings: settings, echoGuard: echoGuard) else { + return PullReport(failure: .pullNotSaved) + } + return PullReport(broughtRemoteChanges: token != nil && result.carriesRemoteChanges(besides: echoGuard)) } catch let error where Self.isTokenExpired(error) { Self.logger.warning("Change token expired, clearing and retrying with full fetch") metadataStorage.saveToken(nil) do { let result = try await transport.pull(since: nil) return await applyPull(result, isFullFetch: true, settings: settings, echoGuard: echoGuard) - ? nil : .pullNotSaved + ? PullReport() : PullReport(failure: .pullNotSaved) } catch { Self.logger.error("Full fetch after token expiry failed: \(error.localizedDescription)") - return SyncStepFailure(error) + return PullReport(failure: SyncStepFailure(error)) } } catch { Self.logger.error("Pull failed: \(error.localizedDescription)") - return SyncStepFailure(error) + return PullReport(failure: SyncStepFailure(error)) } } @@ -1294,6 +1257,7 @@ final class SyncCoordinator: ObservableObject { localChanges: observeLocalChanges(), networkMonitor: observeNetwork() ) + pushChannel.register() /// Empty local storage (a fresh install or a wipe) needs a full fetch: a delta returns nothing. if services.connectionStorage.loadConnections().isEmpty { diff --git a/apple/TablePro/Core/Sync/SyncEchoGuard.swift b/apple/TablePro/Core/Sync/SyncEchoGuard.swift index 0353d7d78b..9a8f6405bb 100644 --- a/apple/TablePro/Core/Sync/SyncEchoGuard.swift +++ b/apple/TablePro/Core/Sync/SyncEchoGuard.swift @@ -41,3 +41,13 @@ struct SyncEchoGuard: Sendable { return tracker.hasEdit(identity, since: snapshot) } } + +extension PullResult { + // Records this run did not upload, which another device sent. + func carriesRemoteChanges(besides echoGuard: SyncEchoGuard?) -> Bool { + let uploaded = echoGuard?.pushedRecordIDs ?? [] + let deleted = echoGuard.map { Set($0.deletedRecords.keys) } ?? [] + return changedRecords.contains { !uploaded.contains($0.recordID) } + || deletedRecordIDs.contains { !deleted.contains($0) } + } +} diff --git a/apple/TablePro/Core/Sync/SyncPushRegistrar+AppKit.swift b/apple/TablePro/Core/Sync/SyncPushRegistrar+AppKit.swift new file mode 100644 index 0000000000..f525e419a1 --- /dev/null +++ b/apple/TablePro/Core/Sync/SyncPushRegistrar+AppKit.swift @@ -0,0 +1,9 @@ +import AppKit +import TableProSyncTransport + +extension SyncPushRegistrar { + static let live = SyncPushRegistrar( + register: { NSApplication.shared.registerForRemoteNotifications() }, + unregister: { NSApplication.shared.unregisterForRemoteNotifications() } + ) +} diff --git a/apple/TablePro/Core/Sync/SyncTransport.swift b/apple/TablePro/Core/Sync/SyncTransport.swift index 0192c05266..f475ebc901 100644 --- a/apple/TablePro/Core/Sync/SyncTransport.swift +++ b/apple/TablePro/Core/Sync/SyncTransport.swift @@ -7,6 +7,7 @@ protocol SyncTransport: Sendable { func accountStatus() async throws -> CKAccountStatus func currentAccountId() async throws -> String func ensureZoneExists() async throws + func saveSubscription() async throws func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome func pull(since token: CKServerChangeToken?) async throws -> PullResult func fetch(recordIDs: [CKRecord.ID]) async throws -> [CKRecord.ID: CKRecord] diff --git a/apple/TablePro/TablePro.entitlements b/apple/TablePro/TablePro.entitlements index e094e5eb3d..c059f6ed08 100644 --- a/apple/TablePro/TablePro.entitlements +++ b/apple/TablePro/TablePro.entitlements @@ -2,6 +2,8 @@ + com.apple.developer.aps-environment + production com.apple.developer.icloud-container-identifiers iCloud.com.TablePro diff --git a/apple/TableProMobile/TableProMobile/AppState.swift b/apple/TableProMobile/TableProMobile/AppState.swift index 7520b2040e..fc2c1244aa 100644 --- a/apple/TableProMobile/TableProMobile/AppState.swift +++ b/apple/TableProMobile/TableProMobile/AppState.swift @@ -5,6 +5,7 @@ import TableProConnectionLibrary import TableProDatabase import TableProLogRedaction import TableProModels +import TableProSyncTransport @MainActor @Observable final class AppState { @@ -220,6 +221,18 @@ final class AppState { publishLibrary() } + // A background wake can come while the device is locked and the library unreadable, so nothing runs until it loads. + func syncFromBackground(_ trigger: SyncTrigger) async -> Bool { + guard onboarding.isCloudSyncEnabled else { return false } + retryLoadIfFailed() + guard loadStatus == .ready else { + Self.logger.warning("Background sync skipped: persistence load not ready (likely device locked)") + return false + } + await syncCoordinator.sync(trigger) + return true + } + private func refuseWriteIfNotReady() -> Bool { guard isLibraryWritable else { Self.logger.error("Refusing a library write while the stored library is not loaded") diff --git a/apple/TableProMobile/TableProMobile/Info.plist b/apple/TableProMobile/TableProMobile/Info.plist index 8428493002..88d57f4c64 100644 --- a/apple/TableProMobile/TableProMobile/Info.plist +++ b/apple/TableProMobile/TableProMobile/Info.plist @@ -57,6 +57,7 @@ UIBackgroundModes fetch + remote-notification BGTaskSchedulerPermittedIdentifiers diff --git a/apple/TableProMobile/TableProMobile/Platform/SceneLifecycle.swift b/apple/TableProMobile/TableProMobile/Platform/SceneLifecycle.swift index 7f65197c10..daa639a3c8 100644 --- a/apple/TableProMobile/TableProMobile/Platform/SceneLifecycle.swift +++ b/apple/TableProMobile/TableProMobile/Platform/SceneLifecycle.swift @@ -1,9 +1,57 @@ import Combine import Observation +import os import SwiftUI +import TableProSyncTransport import UIKit final class TableProAppDelegate: NSObject, UIApplicationDelegate { + // Set in the app's init, which runs before the system hands this delegate a launch or a notification. + static weak var appState: AppState? + + private static let logger = Logger(subsystem: "com.TablePro", category: "Sync") + + // iOS ends an app that has not answered a notification in 30 seconds; a run still going finishes when the app next runs. + private static let notificationBudget: Duration = .seconds(25) + + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { + Self.appState?.syncCoordinator.registerForPushesIfEnabled() + return true + } + + func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: any Error) { + Self.logger.warning("Push registration failed: \(error.localizedDescription, privacy: .private)") + } + + func application( + _ application: UIApplication, + didReceiveRemoteNotification userInfo: [AnyHashable: Any] + ) async -> UIBackgroundFetchResult { + guard SyncPushSubscription.announcesChange(userInfo), let appState = Self.appState else { return .noData } + let run = Task { await appState.syncFromBackground(.remoteChange) } + guard let ran = await Self.value(of: run, within: Self.notificationBudget) else { return .failed } + guard ran else { return .noData } + return appState.syncCoordinator.status.error == nil ? .newData : .failed + } + + private static func value(of task: Task, within budget: Duration) async -> Value? { + await withCheckedContinuation { continuation in + let answer = FirstAnswer(continuation) + let timer = Task { + try? await Task.sleep(for: budget) + answer.give(nil) + } + Task { + let value = await task.value + timer.cancel() + answer.give(value) + } + } + } + func application( _ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, @@ -15,6 +63,19 @@ final class TableProAppDelegate: NSObject, UIApplicationDelegate { } } +private final class FirstAnswer { + private var continuation: CheckedContinuation? + + init(_ continuation: CheckedContinuation) { + self.continuation = continuation + } + + func give(_ value: Value?) { + continuation?.resume(returning: value) + continuation = nil + } +} + enum LockCoverMode: Equatable { case locked case obscured diff --git a/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift b/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift index ffe71de5f7..d9c831c2ef 100644 --- a/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift +++ b/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift @@ -84,6 +84,7 @@ final class IOSSyncCoordinator { @ObservationIgnored private var activationHeld = false @ObservationIgnored private var activationAwaitsRun = false @ObservationIgnored private var downloadIsAheadInRun = false + @ObservationIgnored private let pushChannel: SyncPushChannel // Each returns whether the merged list was saved; false holds the change token for a replay. @ObservationIgnored var onConnectionsChanged: (([DatabaseConnection]) -> Bool)? @@ -109,8 +110,10 @@ final class IOSSyncCoordinator { isEnabled: @escaping () -> Bool = { AppPreferences.isCloudSyncEnabled }, notificationCenter: NotificationCenter = .default, networkMonitor: SyncNetworkMonitor? = SyncNetworkMonitor(), - activationClock: any Clock = ContinuousClock() + activationClock: any Clock = ContinuousClock(), + pushRegistrar: SyncPushRegistrar = .live ) { + self.pushChannel = SyncPushChannel(registrar: pushRegistrar, metadata: metadata) self.metadata = metadata self.recordCache = recordCache self.makeTransport = makeTransport @@ -160,6 +163,12 @@ final class IOSSyncCoordinator { // MARK: - Enable / Disable + // At launch, from the app delegate: nothing registers while sync is off. + func registerForPushesIfEnabled() { + guard isEnabled() else { return } + pushChannel.register() + } + func setEnabled(_ enabled: Bool) { resetRetryState() guard enabled else { @@ -167,6 +176,7 @@ final class IOSSyncCoordinator { debounceTask = nil pendingTriggers = [] endActivationHold() + pushChannel.unregister() metadata.lastSyncDate = nil lastSyncDate = nil decide(.disabled(.userDisabled)) @@ -177,6 +187,8 @@ final class IOSSyncCoordinator { prepareFullUpload() } decide(.idle) + pushChannel.forgetSubscription() + registerForPushesIfEnabled() Task { await sync(.userRequest) } } @@ -194,6 +206,7 @@ final class IOSSyncCoordinator { } private func prepareFullUpload() { + pushChannel.forgetSubscription() metadata.saveToken(nil) metadata.zoneState = .unknown dropRecordCache() @@ -212,7 +225,9 @@ final class IOSSyncCoordinator { return } if let runningSync { - if !pendingTriggers.contains(trigger) { + // The download still ahead in this run brings what a notice announced. + let coveredByRun = trigger == .remoteChange && downloadIsAheadInRun + if !coveredByRun, !pendingTriggers.contains(trigger) { pendingTriggers.append(trigger) } await runningSync.value @@ -367,6 +382,7 @@ final class IOSSyncCoordinator { admission = .full } } + await pushChannel.saveSubscriptionIfNeeded { try await transport.saveSubscription() } guard let result = await pullThenPush(admission, generation: generation, using: transport) else { return nil } let zone = metadata.zoneState @@ -376,6 +392,7 @@ final class IOSSyncCoordinator { if nextZone == .removed, zone != .removed { dropRecordCache() metadata.saveToken(nil) + pushChannel.forgetSubscription() } metadata.zoneState = nextZone return result diff --git a/apple/TableProMobile/TableProMobile/Sync/IOSSyncTransport.swift b/apple/TableProMobile/TableProMobile/Sync/IOSSyncTransport.swift index 13e86f88b2..a07f844dcd 100644 --- a/apple/TableProMobile/TableProMobile/Sync/IOSSyncTransport.swift +++ b/apple/TableProMobile/TableProMobile/Sync/IOSSyncTransport.swift @@ -7,6 +7,7 @@ nonisolated protocol IOSSyncTransport: Sendable { func accountStatus() async throws -> CKAccountStatus func currentAccountId() async throws -> String func ensureZoneExists() async throws + func saveSubscription() async throws func pull(since token: CKServerChangeToken?) async throws -> PullResult func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome func fetch(recordIDs: [CKRecord.ID]) async throws -> [CKRecord.ID: CKRecord] diff --git a/apple/TableProMobile/TableProMobile/Sync/SyncPushRegistrar+UIKit.swift b/apple/TableProMobile/TableProMobile/Sync/SyncPushRegistrar+UIKit.swift new file mode 100644 index 0000000000..17be11ea42 --- /dev/null +++ b/apple/TableProMobile/TableProMobile/Sync/SyncPushRegistrar+UIKit.swift @@ -0,0 +1,9 @@ +import TableProSyncTransport +import UIKit + +extension SyncPushRegistrar { + static let live = SyncPushRegistrar( + register: { UIApplication.shared.registerForRemoteNotifications() }, + unregister: { UIApplication.shared.unregisterForRemoteNotifications() } + ) +} diff --git a/apple/TableProMobile/TableProMobile/TableProMobileApp.swift b/apple/TableProMobile/TableProMobile/TableProMobileApp.swift index dbb9a6dd30..f7a93cad27 100644 --- a/apple/TableProMobile/TableProMobile/TableProMobileApp.swift +++ b/apple/TableProMobile/TableProMobile/TableProMobileApp.swift @@ -21,6 +21,7 @@ struct TableProMobileApp: App { ConnectionListTips.configure() let appState = AppState() _appState = State(initialValue: appState) + TableProAppDelegate.appState = appState // At launch, so a connection opened before the scene first turns active still sends its first-use event. if !TestRuntime.isActive { Self.startTelemetry(appState: appState) @@ -106,15 +107,7 @@ struct TableProMobileApp: App { @Sendable private func runBackgroundSync() async { scheduleBackgroundSync() - guard AppPreferences.isCloudSyncEnabled else { return } - await MainActor.run { appState.retryLoadIfFailed() } - let status = await MainActor.run { appState.loadStatus } - guard status == .ready else { - Self.backgroundLogger.warning("Background sync skipped: persistence load not ready (likely device locked)") - return - } - Self.backgroundLogger.info("Background sync starting") - await appState.syncCoordinator.sync(.activation) + guard await appState.syncFromBackground(.activation) else { return } Self.backgroundLogger.info("Background sync completed") } } diff --git a/apple/TableProMobile/TableProMobile/TableProMobileRelease.entitlements b/apple/TableProMobile/TableProMobile/TableProMobileRelease.entitlements index 3ae2bfe544..169be5c6a7 100644 --- a/apple/TableProMobile/TableProMobile/TableProMobileRelease.entitlements +++ b/apple/TableProMobile/TableProMobile/TableProMobileRelease.entitlements @@ -2,6 +2,8 @@ + aps-environment + development com.apple.developer.icloud-container-identifiers iCloud.com.TablePro diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingPushRegistrar.swift b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingPushRegistrar.swift new file mode 100644 index 0000000000..a05145c22a --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingPushRegistrar.swift @@ -0,0 +1,19 @@ +@testable import TableProMobile +import TableProSyncTransport + +@MainActor +final class RecordingPushRegistrar { + private(set) var registrations = 0 + private(set) var unregistrations = 0 + + var registrar: SyncPushRegistrar { + SyncPushRegistrar( + register: { [weak self] in self?.registrations += 1 }, + unregister: { [weak self] in self?.unregistrations += 1 } + ) + } +} + +extension SyncPushRegistrar { + static let inert = SyncPushRegistrar(register: {}, unregister: {}) +} diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift index 5f662b47a8..0b7da0990f 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift @@ -12,6 +12,8 @@ actor RecordingSyncTransport: IOSSyncTransport { private(set) var pushedRecords: [CKRecord] = [] private(set) var pushedDeletions: [CKRecord.ID] = [] private(set) var pullCount = 0 + private(set) var subscriptionSaves = 0 + private var subscriptionError: (any Error)? private var beforeAccountRead: (@Sendable () async -> Void)? private var beforePull: (@Sendable () async -> Void)? @@ -51,6 +53,17 @@ actor RecordingSyncTransport: IOSSyncTransport { func ensureZoneExists() async throws {} + func failSubscriptionSaves(with error: (any Error)?) { + subscriptionError = error + } + + func saveSubscription() async throws { + subscriptionSaves += 1 + if let subscriptionError { + throw subscriptionError + } + } + func pull(since token: CKServerChangeToken?) async throws -> PullResult { pullCount += 1 await beforePull?() diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/UnreachableSyncTransport.swift b/apple/TableProMobile/TableProMobileTests/Mocks/UnreachableSyncTransport.swift index 69ff123e1f..a14e8a02cb 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/UnreachableSyncTransport.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/UnreachableSyncTransport.swift @@ -19,6 +19,10 @@ struct UnreachableSyncTransport: IOSSyncTransport { func ensureZoneExists() async throws {} + func saveSubscription() async throws { + throw CKError(.notAuthenticated) + } + func pull(since token: CKServerChangeToken?) async throws -> PullResult { PullResult(changedRecords: [], deletedRecordIDs: [], newToken: nil) } diff --git a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift index ebb49e53dd..2fbfbdddbb 100644 --- a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift +++ b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift @@ -58,7 +58,8 @@ struct AppStateFixture { recordCache: SyncRecordCache(directory: root.appendingPathComponent("Cache"), defaults: nil), makeTransport: { transport }, isEnabled: { syncEnabled }, - notificationCenter: NotificationCenter() + notificationCenter: NotificationCenter(), + pushRegistrar: .inert ) return AppState( libraryDirectory: libraryDirectory, diff --git a/apple/TableProMobile/TableProMobileTests/SavedQueries/SavedQueryLibraryTests.swift b/apple/TableProMobile/TableProMobileTests/SavedQueries/SavedQueryLibraryTests.swift index fa4e64e86c..d2bf024349 100644 --- a/apple/TableProMobile/TableProMobileTests/SavedQueries/SavedQueryLibraryTests.swift +++ b/apple/TableProMobile/TableProMobileTests/SavedQueries/SavedQueryLibraryTests.swift @@ -23,7 +23,8 @@ struct SavedQueryLibraryTests { makeTransport: { UnreachableSyncTransport() }, isEnabled: { false }, notificationCenter: NotificationCenter(), - networkMonitor: nil + networkMonitor: nil, + pushRegistrar: .inert ) } diff --git a/apple/TableProMobile/TableProMobileTests/Sync/AppStateSyncWiringTests.swift b/apple/TableProMobile/TableProMobileTests/Sync/AppStateSyncWiringTests.swift index 345bdfc222..ece5284717 100644 --- a/apple/TableProMobile/TableProMobileTests/Sync/AppStateSyncWiringTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Sync/AppStateSyncWiringTests.swift @@ -21,6 +21,19 @@ struct AppStateSyncWiringTests { return state } + @Test("A CloudKit notice downloads through the app's wiring only while sync is on") + func noticeDownloadsOnlyWithSyncOn() async { + let transport = RecordingSyncTransport() + let state = makeSyncedState(transport: transport) + state.onboarding.setSyncChoice(false) + #expect(await state.syncFromBackground(.remoteChange) == false) + #expect(await transport.pullCount == 0) + + state.onboarding.setSyncChoice(true) + #expect(await state.syncFromBackground(.remoteChange)) + #expect(await transport.pullCount == 1) + } + @Test("A saved query made on the Mac reaches the saved queries and the file through the app's sync wiring") func pulledSavedQueryReachesTheLibrary() async { let folder = SavedQueryFolder(name: "Reports") diff --git a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift index edaf804ef3..aec6399396 100644 --- a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift @@ -31,7 +31,8 @@ struct IOSSyncActivationTests { isEnabled: { true }, notificationCenter: NotificationCenter(), networkMonitor: nil, - activationClock: clock + activationClock: clock, + pushRegistrar: .inert ) coordinator.getCurrentState = { ([], [], []) } coordinator.onConnectionsChanged = { _ in true } diff --git a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncCoordinatorTests.swift b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncCoordinatorTests.swift index 801413e2e2..d4e08f766c 100644 --- a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncCoordinatorTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncCoordinatorTests.swift @@ -129,6 +129,8 @@ private actor FakeSyncTransport: IOSSyncTransport { return accountId } + func saveSubscription() async throws {} + func ensureZoneExists() async throws { zoneSaves += 1 } @@ -211,7 +213,8 @@ struct IOSSyncCoordinatorTests { makeTransport: { transport }, isEnabled: { box.syncEnabled }, notificationCenter: notificationCenter, - networkMonitor: nil + networkMonitor: nil, + pushRegistrar: .inert ) coordinator.getCurrentState = { (box.connections, box.groups, box.tags) } coordinator.onConnectionsChanged = { box.connections = $0; return box.saves } diff --git a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncPushTests.swift b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncPushTests.swift new file mode 100644 index 0000000000..6fb1d2668b --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncPushTests.swift @@ -0,0 +1,158 @@ +import CloudKit +import Foundation +@testable import TableProMobile +import TableProModels +import TableProSync +import TableProSyncTransport +import TableProTestSupport +import Testing + +@MainActor +struct IOSSyncPushTests { + private let metadata: SyncMetadataStorage + private let cacheDirectory: URL + private let clock = ManualClock() + private let transport: RecordingSyncTransport + private let registrar = RecordingPushRegistrar() + + init() throws { + transport = RecordingSyncTransport() + let defaults = try #require(UserDefaults(suiteName: "com.TablePro.tests.IOSSyncPush.\(UUID().uuidString)")) + metadata = SyncMetadataStorage(userDefaults: defaults) + _ = metadata.adoptReadableRecordTypes(IOSSyncCoordinator.readableRecordTypes) + cacheDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("ios-sync-push-\(UUID().uuidString)", isDirectory: true) + } + + private func makeCoordinator(isEnabled: @escaping () -> Bool = { true }) -> IOSSyncCoordinator { + let transport = transport + let coordinator = IOSSyncCoordinator( + metadata: metadata, + recordCache: SyncRecordCache(directory: cacheDirectory, defaults: nil), + makeTransport: { transport }, + isEnabled: isEnabled, + notificationCenter: NotificationCenter(), + networkMonitor: nil, + activationClock: clock, + pushRegistrar: registrar.registrar + ) + coordinator.getCurrentState = { ([], [], []) } + coordinator.onConnectionsChanged = { _ in true } + coordinator.onGroupsChanged = { _ in true } + coordinator.onTagsChanged = { _ in true } + coordinator.getSavedQueries = { SavedQueryContents() } + coordinator.onSavedQueriesChanged = { _ in true } + return coordinator + } + + @Test("A change notice downloads at once, inside the minute that holds activations back") + func noticeDownloadsInsideTheInterval() async { + let coordinator = makeCoordinator() + await coordinator.syncOnSceneActivation() + await coordinator.syncOnSceneActivation() + #expect(coordinator.isHoldingActivations) + #expect(await transport.pullCount == 1) + + await coordinator.sync(.remoteChange) + + #expect(await transport.pullCount == 2) + } + + @Test("Notices before a run's download are answered by it") + func noticesBeforeTheDownloadAddNoRun() async { + let coordinator = makeCoordinator() + let gate = AsyncGate() + await transport.holdAccountChecks { await gate.wait() } + let edit = Task { await coordinator.sync(.localChange) } + await gate.arrived(1) + + // Each notice runs before the opener, since all of them share the main actor. + let notices = (0..<5).map { _ in Task { await coordinator.sync(.remoteChange) } } + let opener = Task { await gate.open() } + for notice in notices { + await notice.value + } + await opener.value + await edit.value + + #expect(await transport.pullCount == 1) + } + + @Test("Notices during a run's download add one more run, never one each") + func noticesDuringTheDownloadAddOneRun() async { + let coordinator = makeCoordinator() + let gate = AsyncGate() + await transport.holdPulls { await gate.wait() } + let edit = Task { await coordinator.sync(.localChange) } + await gate.arrived(1) + + await transport.holdPulls {} + let notices = (0..<5).map { _ in Task { await coordinator.sync(.remoteChange) } } + let opener = Task { await gate.open() } + for notice in notices { + await notice.value + } + await opener.value + await edit.value + + #expect(await transport.pullCount == 2) + } + + @Test("With sync off nothing registers, saves a subscription or downloads") + func syncOffDoesNothing() async { + let coordinator = makeCoordinator(isEnabled: { false }) + coordinator.registerForPushesIfEnabled() + await coordinator.sync(.remoteChange) + + #expect(registrar.registrations == 0) + #expect(await transport.subscriptionSaves == 0) + #expect(await transport.pullCount == 0) + } + + @Test("Turning sync on registers once, and turning it off unregisters") + func registrationFollowsTheSwitch() async { + let switchBox = SwitchBox() + let coordinator = makeCoordinator(isEnabled: { switchBox.isOn }) + coordinator.registerForPushesIfEnabled() + coordinator.registerForPushesIfEnabled() + #expect(registrar.registrations == 1) + + switchBox.isOn = false + coordinator.setEnabled(false) + #expect(registrar.unregistrations == 1) + + switchBox.isOn = true + coordinator.setEnabled(true) + #expect(registrar.registrations == 2) + } + + @Test("The subscription is saved once for the account") + func subscriptionIsSavedOnce() async { + let coordinator = makeCoordinator() + await coordinator.sync(.userRequest) + await coordinator.sync(.userRequest) + + #expect(await transport.subscriptionSaves == 1) + #expect(metadata.subscribedAccountId == "recording-account") + } + + @Test("A save that fails on the network leaves the run alone and is tried on the next run") + func transientSaveFailureRetries() async { + await transport.failSubscriptionSaves(with: CKError(.networkFailure)) + let coordinator = makeCoordinator() + await coordinator.sync(.userRequest) + #expect(coordinator.status == .idle) + #expect(await transport.pullCount == 1) + #expect(metadata.subscribedAccountId == nil) + + await transport.failSubscriptionSaves(with: nil) + await coordinator.sync(.userRequest) + #expect(await transport.subscriptionSaves == 2) + #expect(metadata.subscribedAccountId == "recording-account") + } +} + +@MainActor +private final class SwitchBox { + var isOn = true +} diff --git a/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift b/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift index f6cdbc6cc1..b485eb8cd0 100644 --- a/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift +++ b/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift @@ -53,7 +53,8 @@ struct AppSettingsValueColorsTests { makeNetworkMonitor: { nil }, columnLayouts: columnLayouts, themeCatalog: catalog, - licenseGate: { nil } + licenseGate: { nil }, + pushRegistrar: .inert ) subscription = events.valueColorSettingsChanged.sink { [weak self] in self?.sentEvents += 1 } } diff --git a/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift b/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift index 9a058af349..40dc170262 100644 --- a/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift @@ -228,7 +228,8 @@ struct SyncCategoryRefetchTests { transport: server, makeNetworkMonitor: { nil }, columnLayouts: columnLayouts, - licenseGate: { nil } + licenseGate: { nil }, + pushRegistrar: .inert ) } @@ -286,6 +287,8 @@ private actor ZoneServer: SyncTransport { func ensureZoneExists() async throws {} + func saveSubscription() async throws {} + func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome { var outcome = PushOutcome() for record in records { diff --git a/apple/TableProTests/Core/Sync/SyncCoordinatorPushTests.swift b/apple/TableProTests/Core/Sync/SyncCoordinatorPushTests.swift new file mode 100644 index 0000000000..45872c6346 --- /dev/null +++ b/apple/TableProTests/Core/Sync/SyncCoordinatorPushTests.swift @@ -0,0 +1,353 @@ +import CloudKit +import Foundation +@testable import TablePro +import TableProSyncTransport +import TableProTestSupport +import Testing + +@MainActor +struct SyncCoordinatorPushTests { + private let environment: SyncTestEnvironment + private let clock = ManualClock() + private let registrar = RecordingPushRegistrar() + + init() throws { + environment = try SyncTestEnvironment(label: "sync-push") + setSyncEnabled(true) + } + + private func setSyncEnabled(_ enabled: Bool) { + var settings = SyncSettings.default + settings.enabled = enabled + AppSettingsStorage(userDefaults: environment.defaults).saveSync(settings) + } + + private func makeCoordinator(_ transport: any SyncTransport) -> SyncCoordinator { + let base = environment.makeCoordinator(transport: ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID)) + return SyncCoordinator( + services: base.services, + recordCache: environment.recordCache, + transport: transport, + makeNetworkMonitor: { nil }, + columnLayouts: environment.columnLayouts, + licenseGate: { nil }, + activationClock: clock, + pushRegistrar: registrar.registrar + ) + } + + // What CloudKit sends for the database subscription, in the form its own parser reads. + private static func notice(subscriptionID: String = SyncPushSubscription.id) -> [AnyHashable: Any] { + [ + "aps": ["content-available": 1], + "ck": [ + "ce": 2, + "cid": CloudKitSyncEngine.defaultContainerID, + "nid": UUID().uuidString, + "met": ["dbs": 1, "sid": subscriptionID] + ] as [String: Any] + ] + } + + // The launch run of an app whose registration CloudKit answered. + private func launch(_ coordinator: SyncCoordinator, registered: Bool, pulls: @escaping () async -> Int) async throws { + coordinator.start() + coordinator.pushRegistrationDidFinish(registered: registered) + try await waitUntil { await pulls() == 1 } + try await settle(coordinator) + } + + // A run ends in a new hold, or in none when nothing was held. + private func settle(_ coordinator: SyncCoordinator) async throws { + try await waitUntil { + coordinator.syncStatus != .syncing && (clock.pendingSleeperCount > 0 || !coordinator.isHoldingActivations) + } + } + + @Test("A change notice downloads at once, inside the hold that keeps activations back") + func noticeDownloadsInsideTheHold() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + await coordinator.sync(.activation) + #expect(coordinator.isHoldingActivations) + #expect(await transport.pullCount == 1) + + coordinator.handleRemoteNotification(Self.notice()) + + try await waitUntil { await transport.pullCount == 2 } + #expect(clock.now.offset == .zero) + } + + @Test("Notices before a run's download are answered by it") + func noticesBeforeTheDownloadAddNoRun() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + let reads = ZoneReadCount() + // The first zone read of a run comes before its download. + await transport.whileZoneIsRead { + guard reads.next() == 1 else { return } + for _ in 0..<5 { + await coordinator.sync(.remoteChange) + } + } + + await coordinator.sync(.userRequest) + try await settle(coordinator) + + #expect(await transport.pullCount == 1) + } + + @Test("Notices after a run's download began add one more run, never one each") + func noticesDuringTheDownloadAddOneRun() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + let reads = ZoneReadCount() + // The second zone read of a run comes after its download. + await transport.whileZoneIsRead { + guard reads.next() == 2 else { return } + for _ in 0..<5 { + await coordinator.sync(.remoteChange) + } + } + + await coordinator.sync(.userRequest) + try await waitUntil { await transport.pullCount == 2 } + try await settle(coordinator) + + #expect(await transport.pullCount == 2) + } + + @Test("With sync off nothing registers, no subscription is saved, and notices are ignored") + func syncOffDoesNothing() async throws { + setSyncEnabled(false) + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + coordinator.start() + coordinator.pushRegistrationDidFinish(registered: true) + coordinator.handleRemoteNotification(Self.notice()) + await coordinator.sync(.remoteChange) + + #expect(registrar.registrations == 0) + #expect(registrar.unregistrations == 0) + #expect(await transport.subscriptionSaveCount == 0) + #expect(await transport.pullCount == 0) + #expect(!coordinator.isPushLive) + } + + @Test("Turning sync on registers, and turning it off unregisters and ends push") + func registrationFollowsTheSwitch() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + #expect(registrar.registrations == 1) + #expect(coordinator.isPushLive) + + setSyncEnabled(false) + coordinator.disableSync() + #expect(registrar.unregistrations == 1) + #expect(!coordinator.isPushLive) + + setSyncEnabled(true) + coordinator.enableSync() + #expect(registrar.registrations == 2) + #expect(!coordinator.isPushLive) + } + + @Test("The subscription is saved once per account, and again for a new account") + func subscriptionIsSavedOncePerAccount() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + await coordinator.sync(.launch) + await coordinator.sync(.userRequest) + #expect(await transport.subscriptionSaveCount == 1) + #expect(environment.metadata.subscribedAccountId == ScriptedSyncTransport.accountId) + + await transport.setAccountId("another") + await coordinator.sync(.accountChange) + #expect(await transport.subscriptionSaveCount == 2) + #expect(environment.metadata.subscribedAccountId == "another") + } + + @Test("A save that fails leaves the run alone, and activations keep the one minute hold") + func failedSaveKeepsTheMinute() async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + await transport.failSubscriptionSaves(with: CKError(.networkFailure)) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + + #expect(coordinator.syncStatus == .idle) + #expect(!coordinator.isPushLive) + #expect(coordinator.activationHoldInterval == SyncCoordinator.activationInterval) + + await transport.failSubscriptionSaves(with: nil) + await coordinator.sync(.userRequest) + #expect(await transport.subscriptionSaveCount == 2) + #expect(coordinator.isPushLive) + } + + @Test("An activation every 30 s for an hour downloads 60 times without push and 4 times with it", + arguments: [(false, 60), (true, 4)]) + func activationsForAnHour(registered: Bool, downloads: Int) async throws { + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: registered) { await transport.pullCount } + #expect(coordinator.isPushLive == registered) + + for _ in 1..<120 { + clock.advance(by: .seconds(30)) + try await settle(coordinator) + await coordinator.sync(.activation) + try await settle(coordinator) + } + + #expect(await transport.pullCount == downloads) + } + + // A transport whose deltas carry what `remote` holds, the way another Mac's saves arrive. + private func makeRemoteServer(_ remote: RemoteChanges) throws -> ScriptedSyncTransport { + environment.metadata.saveToken(try Self.makeChangeToken()) + let token = try Self.makeChangeToken() + return ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID, echoing: { _, _ in + PullResult(changedRecords: remote.take(), deletedRecordIDs: [], newToken: token) + }) + } + + private func endHold(_ coordinator: SyncCoordinator) async throws { + await clock.waitUntilSleeping() + clock.advance(by: coordinator.activationHoldInterval) + try await waitUntil { !coordinator.isHoldingActivations } + } + + private static var remoteTag: CKRecord { + SyncRecordMapper.toCKRecord(ConnectionTag(name: "from another Mac"), in: SyncTestEnvironment.zoneID) + } + + @Test("An activation that brings a change no notice announced goes back to the minute, and the next notice ends that") + func missedNoticeFallsBackToTheMinute() async throws { + let remote = RemoteChanges() + let transport = try makeRemoteServer(remote) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + #expect(coordinator.activationHoldInterval == SyncCoordinator.pushFallbackInterval) + + remote.put(Self.remoteTag) + try await endHold(coordinator) + await coordinator.sync(.activation) + try await settle(coordinator) + + #expect(await transport.pullCount == 2) + #expect(!coordinator.isPushLive) + #expect(coordinator.activationHoldInterval == SyncCoordinator.activationInterval) + + // Counted synchronously, so a payload the filter let through shows before any run starts. + let noticesBefore = coordinator.pushChannel.noticesReceived + coordinator.handleRemoteNotification(Self.notice(subscriptionID: "other")) + coordinator.handleRemoteNotification(["aps": ["alert": "Hello"]]) + #expect(coordinator.pushChannel.noticesReceived == noticesBefore) + + coordinator.handleRemoteNotification(Self.notice()) + try await waitUntil { await transport.pullCount == 3 } + try await settle(coordinator) + #expect(coordinator.isPushLive) + #expect(await transport.subscriptionSaveCount == 2) + } + + @Test("A notice that lands during an activation's run keeps the changes it brings announced") + func noticeDuringTheActivationKeepsPushLive() async throws { + let remote = RemoteChanges() + let transport = try makeRemoteServer(remote) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + + remote.put(Self.remoteTag) + try await endHold(coordinator) + let reads = ZoneReadCount() + await transport.whileZoneIsRead { + guard reads.next() == 1 else { return } + coordinator.handleRemoteNotification(Self.notice()) + } + await coordinator.sync(.activation) + try await settle(coordinator) + + #expect(coordinator.isPushLive) + #expect(coordinator.activationHoldInterval == SyncCoordinator.pushFallbackInterval) + } + + @Test("A notice whose download failed still announces the change the next activation brings") + func noticeBeforeAFailedDownloadKeepsPushLive() async throws { + let remote = RemoteChanges() + let transport = try makeRemoteServer(remote) + let coordinator = makeCoordinator(transport) + try await launch(coordinator, registered: true) { await transport.pullCount } + + remote.put(Self.remoteTag) + try await endHold(coordinator) + await transport.failPulls(with: CKError(.networkFailure)) + let reads = ZoneReadCount() + await transport.whileZoneIsRead { + guard reads.next() == 1 else { return } + coordinator.handleRemoteNotification(Self.notice()) + } + await coordinator.sync(.userRequest) + #expect(coordinator.syncStatus == .error(.offline)) + + await transport.failPulls(with: nil) + await coordinator.sync(.activation) + try await settle(coordinator) + + #expect(coordinator.isPushLive) + #expect(coordinator.activationHoldInterval == SyncCoordinator.pushFallbackInterval) + } + + private static func makeChangeToken() throws -> CKServerChangeToken { + let archiver = NSKeyedArchiver(requiringSecureCoding: true) + archiver.setClassName("CKServerChangeToken", for: PushTestsChangeTokenArchive.self) + archiver.encode(PushTestsChangeTokenArchive(), forKey: NSKeyedArchiveRootObjectKey) + archiver.finishEncoding() + let token = try NSKeyedUnarchiver.unarchivedObject(ofClass: CKServerChangeToken.self, from: archiver.encodedData) + return try #require(token) + } +} + +@MainActor +private final class ZoneReadCount { + private var count = 0 + + func next() -> Int { + count += 1 + return count + } +} + +// What another device saved since the last fetch, handed out once. +private final class RemoteChanges: @unchecked Sendable { + private let lock = NSLock() + private var records: [CKRecord] = [] + + func put(_ record: CKRecord) { + lock.withLock { records.append(record) } + } + + func take() -> [CKRecord] { + lock.withLock { + defer { records = [] } + return records + } + } +} + +@objc(SyncCoordinatorPushTestsChangeTokenArchive) +private final class PushTestsChangeTokenArchive: NSObject, NSSecureCoding { + static var supportsSecureCoding: Bool { true } + + override init() { + super.init() + } + + required init?(coder: NSCoder) { + super.init() + } + + func encode(with coder: NSCoder) {} +} diff --git a/apple/TableProTests/Core/Sync/SyncCoordinatorSQLFavoritePullTests.swift b/apple/TableProTests/Core/Sync/SyncCoordinatorSQLFavoritePullTests.swift index b4f2821c73..9c6a005d17 100644 --- a/apple/TableProTests/Core/Sync/SyncCoordinatorSQLFavoritePullTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCoordinatorSQLFavoritePullTests.swift @@ -182,7 +182,7 @@ struct SyncCoordinatorSQLFavoritePullTests { welcomeRouter: live.welcomeRouter, telemetry: live.telemetry ) - return SyncCoordinator(services: services, recordCache: recordCache) + return SyncCoordinator(services: services, recordCache: recordCache, pushRegistrar: .inert) } private static func makeChangeToken() throws -> CKServerChangeToken { diff --git a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift index e386631c31..182a671afa 100644 --- a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift @@ -67,7 +67,8 @@ struct SyncCoordinatorThemeTests { makeNetworkMonitor: { nil }, columnLayouts: columnLayouts, themeCatalog: catalog, - licenseGate: { nil } + licenseGate: { nil }, + pushRegistrar: .inert ) } @@ -335,7 +336,8 @@ struct SyncCoordinatorThemeTests { makeNetworkMonitor: { nil }, columnLayouts: columnLayouts, themeCatalog: harness.catalog, - licenseGate: { nil } + licenseGate: { nil }, + pushRegistrar: .inert ) relaunched.start() #expect(harness.metadata.loadToken() != nil) diff --git a/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift b/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift index 40104d1065..11d6d22cd3 100644 --- a/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift +++ b/apple/TableProTests/Core/Sync/SyncFullFetchDeletionTests.swift @@ -173,7 +173,8 @@ struct SyncFullFetchDeletionTests { makeNetworkMonitor: { nil }, columnLayouts: environment.columnLayouts, licenseGate: { nil }, - recordCreationDate: { dates.date(for: $0) } + recordCreationDate: { dates.date(for: $0) }, + pushRegistrar: .inert ) } @@ -255,6 +256,8 @@ private actor ExpiringZoneServer: SyncTransport { func ensureZoneExists() async throws {} + func saveSubscription() async throws {} + // Saves without listing the record in the next fetch, as a fetch that lags the save would. func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome { var outcome = PushOutcome() diff --git a/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift b/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift index ca72933878..091b9fa11e 100644 --- a/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift +++ b/apple/TableProTests/Core/Sync/SyncSavedQueriesRefillTests.swift @@ -99,7 +99,8 @@ struct SyncSavedQueriesRefillTests { transport: transport, makeNetworkMonitor: { nil }, columnLayouts: environment.columnLayouts, - licenseGate: { nil } + licenseGate: { nil }, + pushRegistrar: .inert ) } @@ -153,6 +154,8 @@ private actor RefillZoneServer: SyncTransport { func ensureZoneExists() async throws {} + func saveSubscription() async throws {} + func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome { var outcome = PushOutcome() for record in records { diff --git a/apple/TableProTests/Helpers/SyncTestEnvironment.swift b/apple/TableProTests/Helpers/SyncTestEnvironment.swift index 395ff56e49..4e743ac45f 100644 --- a/apple/TableProTests/Helpers/SyncTestEnvironment.swift +++ b/apple/TableProTests/Helpers/SyncTestEnvironment.swift @@ -84,7 +84,8 @@ final class SyncTestEnvironment { favorites: SQLFavoriteManager? = nil, makeNetworkMonitor: @escaping () -> SyncNetworkMonitor? = { nil }, licenseGate: @escaping @MainActor () -> DisableReason? = { nil }, - activationClock: any Clock = ContinuousClock() + activationClock: any Clock = ContinuousClock(), + pushRegistrar: SyncPushRegistrar = .inert ) -> SyncCoordinator { let live = AppServices.live let services = AppServices( @@ -139,7 +140,25 @@ final class SyncTestEnvironment { makeNetworkMonitor: makeNetworkMonitor, columnLayouts: self.columnLayouts, licenseGate: licenseGate, - activationClock: activationClock + activationClock: activationClock, + pushRegistrar: pushRegistrar + ) + } +} + +extension SyncPushRegistrar { + static let inert = SyncPushRegistrar(register: {}, unregister: {}) +} + +@MainActor +final class RecordingPushRegistrar { + private(set) var registrations = 0 + private(set) var unregistrations = 0 + + var registrar: SyncPushRegistrar { + SyncPushRegistrar( + register: { [weak self] in self?.registrations += 1 }, + unregister: { [weak self] in self?.unregistrations += 1 } ) } } @@ -171,6 +190,8 @@ actor ScriptedSyncTransport: SyncTransport { private(set) var pullCount = 0 private(set) var pushCount = 0 private(set) var zoneSaveCount = 0 + private(set) var subscriptionSaveCount = 0 + private var subscriptionError: (any Error)? /// What the account reads as, what every pushed item fails with, and what a pull throws. Each /// is changeable mid-test, the way the account or the storage changes under a running app. @@ -266,6 +287,17 @@ actor ScriptedSyncTransport: SyncTransport { zoneSaveCount += 1 } + func failSubscriptionSaves(with error: (any Error)?) { + subscriptionError = error + } + + func saveSubscription() async throws { + subscriptionSaveCount += 1 + if let subscriptionError { + throw subscriptionError + } + } + func push(records: [CKRecord], deletions: [CKRecord.ID]) async throws -> PushOutcome { pushCount += 1 pushedRecords.append(contentsOf: records) diff --git a/apple/scripts/ci/check-entitlements.py b/apple/scripts/ci/check-entitlements.py index b4064bb42d..c055c8a1c3 100755 --- a/apple/scripts/ci/check-entitlements.py +++ b/apple/scripts/ci/check-entitlements.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 -"""Check both apps pin the Production CloudKit environment. +"""Check both apps pin the Production CloudKit environment and can receive CloudKit's change notices. -A signed build without the key talks to Development, which the shipped schema never reaches. +A signed build without the environment key talks to Development, which the shipped schema never reaches. +A build without the push key never hears of another device's change. The iOS file holds the development +value Xcode writes, and the App Store export signs it as production. """ from __future__ import annotations @@ -10,20 +12,22 @@ import sys from pathlib import Path -KEY = "com.apple.developer.icloud-container-environment" -ENTITLEMENTS = ( - "TablePro/TablePro.entitlements", - "TableProMobile/TableProMobile/TableProMobileRelease.entitlements", +ENVIRONMENT_KEY = "com.apple.developer.icloud-container-environment" +EXPECTED = ( + ("TablePro/TablePro.entitlements", ENVIRONMENT_KEY, "Production"), + ("TablePro/TablePro.entitlements", "com.apple.developer.aps-environment", "production"), + ("TableProMobile/TableProMobile/TableProMobileRelease.entitlements", ENVIRONMENT_KEY, "Production"), + ("TableProMobile/TableProMobile/TableProMobileRelease.entitlements", "aps-environment", "development"), ) def main() -> int: root = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else Path(__file__).resolve().parents[2] status = 0 - for relative in ENTITLEMENTS: - value = plistlib.loads((root / relative).read_bytes()).get(KEY) - if value != "Production": - print(f"{relative}: {KEY} is {value!r}, expected 'Production'") + for relative, key, expected in EXPECTED: + value = plistlib.loads((root / relative).read_bytes()).get(key) + if value != expected: + print(f"{relative}: {key} is {value!r}, expected {expected!r}") status = 1 return status From 9129dae0ec900ee024ba16654a8ec6adc1357c44 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 18:52:00 +0700 Subject: [PATCH 2/3] docs(changelog): add the sync push fragments --- apple/changelog.d/4266.changed.1.md | 1 + apple/changelog.d/4266.changed.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 apple/changelog.d/4266.changed.1.md create mode 100644 apple/changelog.d/4266.changed.md diff --git a/apple/changelog.d/4266.changed.1.md b/apple/changelog.d/4266.changed.1.md new file mode 100644 index 0000000000..afb61e8948 --- /dev/null +++ b/apple/changelog.d/4266.changed.1.md @@ -0,0 +1 @@ +On iOS, changes from your other devices arrive within seconds while the app is open, and in the background when iOS allows. diff --git a/apple/changelog.d/4266.changed.md b/apple/changelog.d/4266.changed.md new file mode 100644 index 0000000000..12b7a29586 --- /dev/null +++ b/apple/changelog.d/4266.changed.md @@ -0,0 +1 @@ +With iCloud sync on, changes from your other devices arrive within seconds while TablePro runs, in the background too. From ce954386ca480c53a9b273cee2add7ed434387cb Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 18:52:12 +0700 Subject: [PATCH 3/3] docs(changelog): shorten the iOS sync push fragment --- apple/changelog.d/4266.changed.1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apple/changelog.d/4266.changed.1.md b/apple/changelog.d/4266.changed.1.md index afb61e8948..6c71167c7a 100644 --- a/apple/changelog.d/4266.changed.1.md +++ b/apple/changelog.d/4266.changed.1.md @@ -1 +1 @@ -On iOS, changes from your other devices arrive within seconds while the app is open, and in the background when iOS allows. +On iOS, changes from your other devices arrive within seconds while the app is open or iOS wakes it.