ios – Understanding Cloudkit record refrences


I have a basic understanding of cloudkit containers and that public databses can’t have zones but am having trouble understanding what rules apply to references to other records.

I have a record AccountInfo in a private zone which has a list of references to Wine records in the public database. When I create the a UserObj from the AccountInfo record like so:

public class UserObj: ObservableObject {
    var Fname: String
    var Lname: String
    var username: String
    var password: String
    var email: String
    var phoneNumber: String
    var pfp: UIImage
    var wines: [Wine]
    var recordID: String
    
    /**
     Creating UserObj from CK Record
     */
    init (ckrecord: CKRecord) async throws {
        self.Fname = ckrecord["FName"] as! String
        self.Lname = ckrecord["LName"] as! String
        self.username = ckrecord["Username"] as! String
        self.password = ckrecord["Password"] as! String
        self.email = ckrecord["Email"] as! String
        self.phoneNumber = ckrecord["PhoneNumber"] as! String
        
        //convert CKAsset to UIImage for pfp
        let imageAsset = ckrecord["PFP"] as! CKAsset
        var imageData = Data()
        do {
            imageData = try Data(contentsOf: imageAsset.fileURL!)
        }
        catch {
            print(error)
        }
        self.pfp = UIImage(data: imageData)!
        //need to set this here to use self. in  async context
        self.recordID = ckrecord.recordID.recordName
        //convert list of references to array of wines
        //REFERENCE(List) in CloudDB stores a list of CKRecord.recordName's
        //Get the list of refrences as an array of strings
        //MARK: Figure out querying a reference
        let wineRefs = ckrecord["Wines"] as! [CKRecord.Reference]
        self.wines = []
        let cloudDB = CKContainer.default().publicCloudDatabase
        Task {
            do {
                //For each wine refrenced query them and create a 'Wine' object out of the refrence
                for wine in wineRefs {
                    let fetchedRecord = try await cloudDB.record(for: wine.recordID)
                    // Handle the fetched record
                    self.wines.append(try await Wine(fetchedRecord))
                }
                
            } catch {
                print("Error fetching record: \(error.localizedDescription)")
            }
        }
    }
}

I receive this error:

Error fetching record: Error fetching record <CKRecordID: 0xd84b835d0; recordName=E65602D5-8A9D-4DDE-A323-C2F6327A756E, zoneID=AccountInformation:defaultOwner> from server: Custom zones are not allowed in public DB

I am confused because only the Account Info record is private not the wine record, so can someone provide insight to how to properly execute the query to create the wine objects?

For more context here is the wine class:

class Wine: Identifiable{
    var type: String
    var vintage: String
    var vineyard: Vineyard
    let id: UUID //to make wine conform to identifiable
    
    init(_ ckrecord: CKRecord) async throws {
        self.type = ckrecord["Type"] as! String
        self.vintage = ckrecord["Vintage"] as! String
        //set as empty in order to use self in async
        self.vineyard = Vineyard()
        self.id = UUID()
        //create vineyard from record
        let vineyardRef = ckrecord["Vineyard"] as! CKRecord.Reference
        let cloudDB = CKContainer.default().publicCloudDatabase
        Task {
            do {
                let fetchedRecord = try await cloudDB.record(for: vineyardRef.recordID)
                // Handle the fetched record
                self.vineyard = Vineyard(fetchedRecord)
            } catch {
                print("Error fetching record: \(error.localizedDescription)")
            }
        }
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img