TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Cloud kit Exception

Forums > SwiftUI

Hello :), I'm working on a to-do app that uses geographic zones to alert you to tasks and stuff. I'm using CloudKit to share the zone and task lists on my personal cloud, but it's not working and I don't really understand my mistake... can you help me? (for your information, I simply hide the identifier for CK Container).

the error : Terminating app due to uncaught exception 'CKException', reason: 'Array members must conform to CKRecordValue: ( "<CKRecord: 0x10e40d9c0; recordType=task, recordID=399C2D65-ABD7-430A-8A0A-3A9DDE4AE2F7:(_defaultZone:defaultOwner), values={\n isCompleted = 0;\n name = \"\";\n}>" ) (CKRecord)' First throw call stack:

the code :

import UIKit
import CoreLocation
import CloudKit

enum ZoneRecordKeys: String {
    case id
    case type = "Zone"
    case name
    case radius
    case color
    case description
    case longitude
    case latitude
    case tasks
}

struct Zone: Identifiable {

    var id: String
    var recordId: CKRecord.ID?
    var name: String
    var radius: Int
    var color: String
    var description: String
    var longitude: Double
    var latitude: Double
    var tasks: [ZoneTask]
}

extension Zone {
    init?(record : CKRecord) {
        guard let id = record[ZoneRecordKeys.id.rawValue] as? String,
              let name = record[ZoneRecordKeys.name.rawValue] as? String,
              let radius = record[ZoneRecordKeys.radius.rawValue] as? Int,
              let color = record[ZoneRecordKeys.color.rawValue] as? String,
              let description = record[ZoneRecordKeys.description.rawValue] as? String,
              let longitude = record[ZoneRecordKeys.longitude.rawValue] as? Double,
              let latitude = record[ZoneRecordKeys.latitude.rawValue] as? Double,
              let tasks = record[ZoneRecordKeys.tasks.rawValue] as? NSArray else {
            return nil

        }
        let zoneTasks: [ZoneTask] = tasks.compactMap {
                    guard let taskRecord = $0 as? CKRecord else { return nil }
                    if let zoneTask = ZoneTask(record: taskRecord) {
                        return zoneTask
                    } else {
                        print("Error converting task record to ZoneTask: \(taskRecord)")
                        return nil
                    }
                }
        self.init(id: id, recordId: record.recordID, name: name, radius: radius, color: color, description: description, longitude: longitude, latitude: latitude, tasks: zoneTasks)
    }
}
extension Zone {
    var record: CKRecord {
        let record = CKRecord(recordType: ZoneRecordKeys.type.rawValue, recordID: CKRecord.ID(recordName: id))
        record[ZoneRecordKeys.id.rawValue] = id
        record[ZoneRecordKeys.name.rawValue] = name
        record[ZoneRecordKeys.radius.rawValue] = radius
        if let color = try? NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false) {
            record[ZoneRecordKeys.color.rawValue] = color
        }
        record[ZoneRecordKeys.description.rawValue] = description
        record[ZoneRecordKeys.longitude.rawValue] = longitude
        record[ZoneRecordKeys.latitude.rawValue] = latitude
        record[ZoneRecordKeys.tasks.rawValue] = NSArray(array: tasks.map { $0.record })
        return record
    }
}

extension Zone {
    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }

        if ((cString.count) != 6) {
            return UIColor.gray
        }

        var rgbValue:UInt64 = 0
        Scanner(string: cString).scanHexInt64(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }
}
import Foundation
import CloudKit

enum ZoneTaskRecordKeys: String {
    case id
    case type = "task"
    case name
    case isCompleted
}

struct ZoneTask: Identifiable {

    var id: String
    var name: String
    var isCompleted: Bool
}

extension ZoneTask {

    init?(record: CKRecord) {
        guard let id = record[ZoneTaskRecordKeys.id.rawValue] as? String,
              let name = record[ZoneTaskRecordKeys.name.rawValue] as? String,
              let isCompleted = record[ZoneTaskRecordKeys.isCompleted.rawValue] as? Bool
        else {
            return nil
        }
        self.init(id: id, name: name, isCompleted: isCompleted)
    }

}

extension ZoneTask {
    var record: CKRecord {
        let record = CKRecord(recordType: ZoneTaskRecordKeys.type.rawValue, recordID: CKRecord.ID(recordName: id))
        record[ZoneTaskRecordKeys.name.rawValue] = name
        record[ZoneTaskRecordKeys.isCompleted.rawValue] = isCompleted
        return record
    }
}
import CloudKit
import UIKit

@MainActor

class CloudKitManager: ObservableObject {

    private var db = CKContainer(identifier: "iCloud.io").privateCloudDatabase
    private var zonesDictionary: [CKRecord.ID: Zone] = [:]

    init(db: CKDatabase = CKContainer(identifier: "iCloud.io").privateCloudDatabase, zonesDictionary: [CKRecord.ID : Zone] = [:]) {
        self.db = db
        self.zonesDictionary = zonesDictionary
    }
    var zones: [Zone] {
        zonesDictionary.values.compactMap { $0 }
    }
    func addZone(zone: Zone) async throws {
        let record = try await db.save(zone.record)
        guard let savedZone = Zone(record: record) else { return }
        zonesDictionary[savedZone.recordId!] = savedZone
    }
    func fetchZones() async throws {
        let query = CKQuery(recordType: ZoneRecordKeys.type.rawValue, predicate: NSPredicate(value: true))
        query.sortDescriptors = [NSSortDescriptor(key: "name", ascending: false)]
        let result = try await db.records(matching: query)
        let records = result.matchResults.compactMap { try? $0.1.get() }
        records.forEach { record in
            zonesDictionary[record.recordID] = Zone(record: record)
        }
    }
    func deleteZone(zoneId: CKRecord.ID) async throws {
        zonesDictionary.removeValue(forKey: zoneId)

        do {
            let _ = try await db.deleteRecord(withID: zoneId)
        } catch {
            print("Error deleting zone: \(error)")
        }
    }

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.