added safecheck

This commit is contained in:
Francesco 2025-04-25 18:00:55 +02:00
parent af2dc41f76
commit a822f05934

View file

@ -95,20 +95,23 @@ class iCloudSyncManager {
}
}
private func syncToiCloud() {
let iCloud = NSUbiquitousKeyValueStore.default
let defaults = UserDefaults.standard
func syncToiCloud(item: SyncItem) {
let queue = DispatchQueue(label: "me.cranci.sora.icloud-sync")
do {
for key in allKeysToSync() {
if let value = defaults.object(forKey: key) {
if isValidValueType(value) {
iCloud.set(value, forKey: key)
}
queue.async {
do {
let container = NSUbiquitousKeyValueStore.default
let encoder = JSONEncoder()
let data = try encoder.encode(item)
if let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
container.set(dict, forKey: "syncedItems")
container.synchronize()
}
} catch {
Logger.shared.log("Failed to sync to iCloud: \(error.localizedDescription)", type: "Error")
}
iCloud.synchronize()
}
}