@@ -33,7 +33,6 @@ public class UserCollection: ObservableObject {
33
33
private let encoder = JSONEncoder ( )
34
34
private let decoder = JSONDecoder ( )
35
35
36
- private static let recordZone = " UserZone "
37
36
private static let recordType = " UserCollection "
38
37
private static let recordId = CKRecord . ID ( recordName: " CurrentUserCollection " )
39
38
private static let assetKey = " data "
@@ -136,19 +135,8 @@ public class UserCollection: ObservableObject {
136
135
}
137
136
138
137
private func subscribeToCloudKit( ) {
139
- let zone = CKRecordZone ( zoneName: Self . recordZone)
140
- cloudKitDatabase? . save ( zone) { ( _, _) in }
141
-
142
- cloudKitDatabase? . fetchAllSubscriptions { ( sub, _) in
143
- if let sub = sub? . first {
144
- let notif = CKSubscription . NotificationInfo ( )
145
- notif. shouldSendContentAvailable = true
146
- sub. notificationInfo = notif
147
-
148
- let operation = CKModifySubscriptionsOperation ( subscriptionsToSave: [ sub] ,
149
- subscriptionIDsToDelete: nil )
150
- self . cloudKitDatabase? . add ( operation)
151
- } else {
138
+ cloudKitDatabase? . fetchAllSubscriptions { ( subs, _) in
139
+ if subs == nil || subs? . isEmpty == true {
152
140
self . createSubscription ( )
153
141
}
154
142
}
@@ -166,14 +154,20 @@ public class UserCollection: ObservableObject {
166
154
167
155
public func reloadFromCloudKit( ) {
168
156
cloudKitDatabase? . fetch ( withRecordID: Self . recordId) { ( record, error) in
169
- self . currentRecord = record
170
- if let asset = record ? [ Self . assetKey] as? CKAsset ,
171
- let url = asset. fileURL {
157
+ if record == nil {
172
158
DispatchQueue . main. async {
173
- self . isSynched = true
174
- _ = self . loadCollection ( file: url)
175
- try ? FileManager . default. removeItem ( at: self . filePath)
176
- try ? FileManager . default. copyItem ( at: url, to: self . filePath)
159
+ self . save ( )
160
+ }
161
+ } else {
162
+ self . currentRecord = record
163
+ if let asset = record ? [ Self . assetKey] as? CKAsset ,
164
+ let url = asset. fileURL {
165
+ DispatchQueue . main. async {
166
+ self . isSynched = true
167
+ _ = self . loadCollection ( file: url)
168
+ try ? FileManager . default. removeItem ( at: self . filePath)
169
+ try ? FileManager . default. copyItem ( at: url, to: self . filePath)
170
+ }
177
171
}
178
172
}
179
173
}
@@ -197,8 +191,10 @@ public class UserCollection: ObservableObject {
197
191
record [ Self . assetKey] = asset
198
192
199
193
cloudKitDatabase? . save ( record) { ( record, error) in
200
- self . currentRecord = record
201
- self . isSynched = true
194
+ DispatchQueue . main. async {
195
+ self . currentRecord = record
196
+ self . isSynched = true
197
+ }
202
198
}
203
199
}
204
200
}
0 commit comments