-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathSyncEngineDelegate.swift
More file actions
134 lines (130 loc) · 4.74 KB
/
Copy pathSyncEngineDelegate.swift
File metadata and controls
134 lines (130 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#if canImport(CloudKit)
import CloudKit
import CustomDump
/// An interface for observing ``SyncEngine`` events and customizing ``SyncEngine`` behavior.
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public protocol SyncEngineDelegate: AnyObject, Sendable {
/// An event indicating a change to the device's iCloud account.
///
/// By default, a sync engine will clear out local data when detecting a logout or account
/// change. To override this behavior, _e.g._ if you want to prompt the user and let them decide
/// if they want to clear their local data or not, implement this method, and explicitly call
/// ``SyncEngine/deleteLocalData()`` if/when the data should be cleared.
///
/// For example, an observable model could override this method to set up some alert state:
///
/// ```swift
/// @MainActor
/// @Observable
/// class MySyncEngineDelegate: SyncEngineDelegate {
/// var isResetDataAlertPresented = false
///
/// func syncEngine(
/// _ syncEngine: SyncEngine,
/// accountChanged changeType: CKSyncEngine.Event.AccountChange.ChangeType
/// ) {
/// switch changeType {
/// case .signOut, .switchAccounts:
/// isResetDataAlertPresented = true
/// case .signIn:
/// break
/// }
/// }
/// }
/// ```
///
/// And then SwiftUI could drive an alert with this state:
///
/// ```swift
/// struct MyApp: App {
/// @State var syncEngineDelegate = MySyncEngineDelegate()
///
/// init() {
/// prepareDependencies {
/// try! $0.bootstrapDatabase(syncEngineDelegate: syncEngineDelegate)
/// }
/// }
///
/// var body: some Scene {
/// WindowGroup {
/// MyRootView()
/// .alert(
/// "Reset local data?",
/// isPresented: $syncEngineDelegate.isDeleteLocalDataAlertPresented
/// ) {
/// Button("Reset", role: .destructive) {
/// Task {
/// try await syncEngine.deleteLocalData()
/// }
/// }
/// } message: {
/// Text(
/// """
/// You are no longer logged into iCloud. Would you like to reset your local data \
/// to the defaults? This will not affect your data in iCloud.
/// """
/// )
/// }
/// }
/// }
/// }
/// ```
///
/// - Parameters:
/// - syncEngine: The sync engine that generates the event.
/// - changeType: The iCloud account's change type.
func syncEngine(
_ syncEngine: SyncEngine,
accountChanged changeType: CKSyncEngine.Event.AccountChange.ChangeType
) async
/// An event indicating that the iCloud database associated with `scope` is full and cannot
/// store any more records.
///
/// You can use this method to be notified when records can no longer be stored in the user's
/// iCloud database. The `scope` argument determines which database is full:
///
/// * If `scope` is `.private`, then the currently logged in user's database is full, and you
/// can let the user know they need to clear up space on their iCloud account or upgrade for
/// more storage.
/// * If the `scope` is `.shared`, then an external user has shared a record with the logged
/// in user, and _their_ iCloud storage is full. You can let the user know that they may want
/// to contact the owner about upgrading their storage or cleaning up their iCloud account.
///
/// This method can be called many times, and so you will want to de-duplicate the
/// `quotaExceeded` boolean so as to not alert your users multiple times.
///
/// - Parameters:
/// - syncEngine: The sync engine that generates the event.
/// - quotaExceeded: Determines if records failed to save due to a 'quotaExceeded` error.
/// - scope: The database that the event occured on.
func syncEngine(
_ syncEngine: SyncEngine,
quotaExceeded: Bool,
scope: CKDatabase.Scope
)
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension SyncEngineDelegate {
public func syncEngine(
_ syncEngine: SyncEngine,
accountChanged changeType: CKSyncEngine.Event.AccountChange.ChangeType
) async {
switch changeType {
case .signOut, .switchAccounts:
await withErrorReporting {
try await syncEngine.deleteLocalData()
}
case .signIn:
break
@unknown default:
break
}
}
public func syncEngine(
_ syncEngine: SyncEngine,
quotaExceeded: Bool,
scope: CKDatabase.Scope
) {
}
}
#endif