I want to clear all the data in the zone, but I can't delete it. Below is my code, no logs are printed when running.
static func clearData() {
let coordinator = shared.container.persistentStoreCoordinator
let fm = FileManager.default
for d in shared.container.persistentStoreDescriptions {
guard let url = d.url else { continue }
do {
if fm.fileExists(atPath: url.path()) {
try coordinator.destroyPersistentStore(at: url, type: .sqlite)
}
} catch {
logger.debug("Failed to delete db file, \(error)")
}
}
for description in shared.container.persistentStoreDescriptions {
guard let originalStoreURL = description.url else { continue }
let walFileURL = originalStoreURL.deletingPathExtension().appendingPathExtension("sqlite-wal")
let shmFileURL = originalStoreURL.deletingPathExtension().appendingPathExtension("sqlite-shm")
for url in [originalStoreURL, walFileURL, shmFileURL] {
do {
if fm.fileExists(atPath: url.path()) {
try fm.removeItem(at: url)
}
} catch {
logger.debug("Failed to delete db file, \(error)")
}
}
}
let container = CKContainer(identifier: appContainerID)
container.privateCloudDatabase.fetchAllRecordZones { zones, error in
if let error = error {
print("Error fetching zones: ")
} else if let zone = zones?.first, zone.zoneID.zoneName == "_defaultZone" {
PersistenceController.shared.container.purgeObjectsAndRecordsInZone(with: zone.zoneID, in: nil) { ckShare, error in
if let error = error {
print("Error purge zones: \(error)")
}
if ckShare == nil {
print("ckShare is nil")
}
}
}
}
}