Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / apple / Shared / BackgroundSync.swift

BackgroundSync.swift65 lines · 3.3 KiBRaw
1// EverySync Apple shells — background scheduling notes.
2//
3// The KMP `SyncScheduler` interface (`schedulePeriodic / cancel / syncNow`)
4// is implemented in KOTLIN (`IOS_ACTUALS_NOTES.md`), not here. This file is
5// the Swift registration side: `BGTaskScheduler` handlers that wake the
6// XCFramework's `SyncDriver` cycles. Scheduling only — cycles run in Kotlin.
7//
8// APNs discipline (verbatim from the plan): APNs is a PULL TRIGGER ONLY,
9// never payload. A push wakes the app; the app then pulls the relay channel
10// (`pullRecords` from `since` cursor) through the XCFramework. No vault data,
11// keys, or envelopes ever ride the push.
12//
13// Identifiers (must match Info.plist `BGTaskSchedulerPermittedIdentifiers` —
14/// see each app's `Info.plist.reference`):
15// - Passwords: `com.vayunmathur.everysync.passwords.refresh` (app-refresh) +
16// one-shot `BGProcessingTask` after local edits.
17// - Notes: `com.vayunmathur.everysync.notes.refresh` + one-shot after edits.
18// - Hub: `com.vayunmathur.everysync.hub.refresh` (app-refresh) +
19// `com.vayunmathur.everysync.hub.upload` (processing, after local
20// system-store changes observed via `EKEventStore` notifications).
21//
22// MAC-CHECK (needs xcodebuild on a Mac):
23// - `BGTaskScheduler.shared.register` identifiers match the plist.
24// - `UIBackgroundModes` includes `fetch` + `processing`.
25// - `BGAppRefreshTaskRequest.earliestBeginDate` throttling honored (system may
26// defer; the hub re-schedules at the END of each task — see below).
27
28import BackgroundTasks
29import Foundation
30
31/// Registers the app's BGTask handlers. Call once at launch (before the
32/// XCFramework creates its `SyncScheduler` actual — the Kotlin actual calls
33/// `submit`/`cancel`, this side only handles the wakeup).
34///
35/// - Parameter handlers: task identifier -> wakeup closure. The closure MUST:
36/// 1. Create its task-specific work (call into the XCFramework driver).
37/// 2. Re-schedule the next refresh (system gives no repeating schedule).
38/// 3. Call `task.setTaskCompleted(success:)` exactly once.
39public enum BackgroundSync {
40 public static func register(handlers: [String: @Sendable @escaping (BGTask) -> Void]) {
41 for (identifier, handler) in handlers {
42 BGTaskScheduler.shared.register(forTaskWithIdentifier: identifier, using: nil, launchHandler: handler)
43 }
44 }
45
46 /// Schedules the next periodic pull. Kotlin's `SyncScheduler.actual`
47 /// calls this shape through (interval from `schedulePeriodic`).
48 public static func scheduleRefresh(identifier: String, intervalMinutes: Int = 15) throws {
49 let request = BGAppRefreshTaskRequest(identifier: identifier)
50 request.earliestBeginDate = Date(timeIntervalSinceNow: TimeInterval(intervalMinutes * 60))
51 try BGTaskScheduler.shared.submit(request)
52 }
53
54 /// One-shot upload after local edits (shells call this; the actual cycle
55 /// still runs in the XCFramework via `SyncScheduler.syncNow`).
56 public static func scheduleProcessing(identifier: String, requiresNetwork: Bool = true) throws {
57 let request = BGProcessingTaskRequest(identifier: identifier)
58 request.requiresNetworkConnectivity = requiresNetwork
59 try BGTaskScheduler.shared.submit(request)
60 }
61
62 public static func cancel(identifier: String) {
63 BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: identifier)
64 }
65}