Search code examples
swiftxcodeswift3cloudkit

Is it possible to create a class that will imitate a CKRecord?


So I'm creating an app that will be reusing some records a lot, so I came up with an idea to do that:

    import CloudKit


class Class: CKRecord {

    override init(recordType: String = "Class") {
        super.init(recordType: recordType)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var name: String? {
        get {
            return value(forKey: "name") as? String
        } set {
            setValue(newValue!, forKey: "name")
        }
    }

    var classDescription: String? {
        get {
            return value(forKey: "description") as? String
        } set {
            setValue(newValue!, forKey: "description")
        }
    }

    var posts: [CKReference]? {
        get {
            return value(forKey: "posts") as? [CKReference]
        } set {
            setValue(newValue!, forKey: "posts")
        }
    }

    var users: [CKReference]? {
        get {
            return value(forKey: "users") as? [CKReference]
        } set {
            setValue(newValue!, forKey: "users")
        }
    }

}

later I created a function in tableViewCOntroler that is run when viewDidLoad is executed and when tableView is refreshed (getAllRecords() is a function that returns all the class records in this cas and it works):

func refresh() {
    queue.addOperation {
        self.database.getAllRecords(withRecordType: "Class", withDesiredKeys: ["posts", "name"], sortForkey: "name", ascending: false, withResultLimit: CKQueryOperationMaximumResults, operations: 1){ records, error in
            if error == nil {
                self.classes = records as! [Class]
                OperationQueue.main.addOperation {
                    self.tableView.reloadData()
                    self.refreshControl?.endRefreshing()
                }
            }
        }
    }
}

And then I created cells:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "class", for: indexPath)

    // Configure the cell...
    cell.textLabel?.text = (classes[indexPath.row].name)!

    return cell
}

The problem is now that I got an error message that says:

fatal error: Down-casted Array element failed to match the target type 2016-12-23 22:44:38.681456 EdApp[4524:2186068] fatal error: Down-casted Array element failed to match the target type

What I did wrong? Can it even work? Thank You!


Solution

  • The getAllRecords will return an array of CKRecords. It will not be of type Class. (Which is a very bad name for a class) You cannot just cast it to a Class.

    You could map it to your Class, but it won't be easy. You would need to get the encodedSystemFields from the CKRecord and create your class using a coder.

    I think it would be easier to make the CKRecord a property of your Class and create a init for your Class that accepts that CKRecord. You could then map your records to a Class with something like: self.classes = records.map { Class(record: $0) }

    Can I sugest you have a look at https://github.com/evermeer/EVCloudKitDao It has a mechanism to automatically convert a class to a CKRecord and back. It will eliminate a lot of your plumbing code for getting and setting the properties.