How do I retrieve CloudKit Reference List?

268 views Asked by At

I have setup my CloudKit record types the same as my local structs and models in my code but can't seem to find a way to retrieve the list of references.

For example, I have a UserData record type with a record field of friends which is a list of references to Friend record type.

Local models:

struct User {
    let id: UUID
    let friends: [Friend]
    let username: String
}
struct Friend {
    let id: UUID
    let username: String
}

And this is the code I use to attempt to retrieve the friends

@State var friends = [Friend]()

        if let userID = UserDefaults.standard.string(forKey: "userID") {
            let publicDatabase = CKContainer.default().publicCloudDatabase
            publicDatabase.fetch(withRecordID: CKRecord.ID(recordName: userID)) { (record, error) in
                if let fetchedInfo = record {
                    friends = (fetchedInfo["friends"] as? [Friend])!
                    
                }
            }
        }

So I guess the main question is how do I retrieve a list record field from CloudKit as it does not seem to return the value as an array.

0

There are 0 answers