@@ -112,6 +112,7 @@ class CloudKitManager {
112
112
CloudKitHostedDatabase . RecordKeys. nickname,
113
113
CloudKitHostedDatabase . RecordKeys. filename,
114
114
CloudKitHostedDatabase . RecordKeys. modDate,
115
+ CloudKitHostedDatabase . RecordKeys. deletedAt,
115
116
CKRecord . SystemFieldKey. share,
116
117
]
117
118
@@ -148,11 +149,12 @@ class CloudKitManager {
148
149
CloudKitHostedDatabase . RecordKeys. nickname,
149
150
CloudKitHostedDatabase . RecordKeys. filename,
150
151
CloudKitHostedDatabase . RecordKeys. modDate,
152
+ CloudKitHostedDatabase . RecordKeys. deletedAt,
151
153
CKRecord . SystemFieldKey. share,
152
154
153
155
]
154
156
155
- return try await executeCloudKitOperation ( theDatabase: cloudKitDatabase) { database in
157
+ let all = try await executeCloudKitOperation ( theDatabase: cloudKitDatabase) { database in
156
158
var all : [ CloudKitHostedDatabase ] = [ ]
157
159
158
160
let ( results, initialQueryCursor) = try await database. records ( matching: query, inZoneWith: zone. zoneID, desiredKeys: desiredKeys)
@@ -178,6 +180,34 @@ class CloudKitManager {
178
180
179
181
return all
180
182
}
183
+
184
+
185
+
186
+ maintenanceDeleteOldDatabasesMarkedDeletedAt ( all)
187
+
188
+ return all
189
+ }
190
+
191
+ func maintenanceDeleteOldDatabasesMarkedDeletedAt( _ databases: [ CloudKitHostedDatabase ] ) {
192
+ let olds = databases. filter { ckDb in
193
+ if !ckDb. sharedWithMe, let deletedAt = ckDb. deletedAt, ( deletedAt as NSDate ) . isMoreThanXDaysAgo ( 90 ) {
194
+ return true
195
+ } else {
196
+ return false
197
+ }
198
+ }
199
+
200
+ if !olds. isEmpty {
201
+ Task {
202
+ for old in olds {
203
+ do {
204
+ try await deleteActual ( id: old. id)
205
+ } catch {
206
+ swlog ( " 🔴 Cannot cleanup an old database [ \( old) ] marked deleted more than 90 days ago. " )
207
+ }
208
+ }
209
+ }
210
+ }
181
211
}
182
212
183
213
func updateDatabase( _ id: CloudKitDatabaseIdentifier , dataBlob: Data ) async throws -> CloudKitHostedDatabase {
@@ -195,6 +225,22 @@ class CloudKitManager {
195
225
}
196
226
197
227
func delete( id: CloudKitDatabaseIdentifier ) async throws {
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+ let existing = try await getDatabase ( id: id, includeDataBlob: false )
236
+
237
+
238
+
239
+
240
+ _ = try await saveOrUpdate ( existing. associatedCkRecord, sharedWithMe: id. sharedWithMe, modDate: . now, dataBlob: Data ( ) , deletedAt: . now)
241
+ }
242
+
243
+ func deleteActual( id: CloudKitDatabaseIdentifier ) async throws {
198
244
guard !id. sharedWithMe else {
199
245
swlog ( " 🔴 Cannot delete a database we don't own " )
200
246
throw CloudKitManagerError . invalidParameters
@@ -207,13 +253,16 @@ class CloudKitManager {
207
253
}
208
254
}
209
255
210
- private func saveOrUpdate( _ ckRecord: CKRecord , sharedWithMe: Bool , nickName: String ? = nil , fileName: String ? = nil , modDate: Date ? = nil , dataBlob: Data ? = nil ) async throws -> CloudKitHostedDatabase {
256
+ private func saveOrUpdate( _ ckRecord: CKRecord , sharedWithMe: Bool , nickName: String ? = nil , fileName: String ? = nil , modDate: Date ? = nil , dataBlob: Data ? = nil , deletedAt : Date ? = nil ) async throws -> CloudKitHostedDatabase {
211
257
if let nickName {
212
258
ckRecord. encryptedValues [ CloudKitHostedDatabase . RecordKeys. nickname] = nickName
213
259
}
214
260
if let fileName {
215
261
ckRecord. encryptedValues [ CloudKitHostedDatabase . RecordKeys. filename] = fileName
216
262
}
263
+ if let deletedAt {
264
+ ckRecord. encryptedValues [ CloudKitHostedDatabase . RecordKeys. deletedAt] = deletedAt
265
+ }
217
266
218
267
var tmpAssetUrlToDelete : URL ? = nil
219
268
0 commit comments