iosswiftcloudkitckrecordckrecordzone

How to assign a CKRecordZone to a CKRecord


I am trying to make a database in which an admin user can create a group which is stored as a group CKRecord type. I want this record to be placed a custom zone that is named after the group name that the admin user decided on. How do I initialize the group private Record to be assigned to that custom zone (without using the deprecated function).

func createGroup(groupName: String){

    let groupRecord = CKRecord(recordType: recordTypes.group)


    groupRecord[groupRecordType.groupName] = groupName
    let share = CKShare(rootRecord: groupRecord)
    share.publicPermission = .readWrite

    let groupZone = CKRecordZone(zoneName: groupName)

    //Need to somehow connect the groupZone to the groupRecord

 }

Solution

  • You can use a different CKShare initializer:

    CKShare(rootRecord: CKRecord, shareID: CKRecord.ID)
    

    The recordZone is part of the CKRecord.ID:

    CKRecord.ID(recordName: String, zoneID: CKRecordZone.ID)
    

    So something more like:

    func createGroup(groupName: String){
    
        let groupRecord = CKRecord(recordType: recordTypes.group)
    
        groupRecord[groupRecordType.groupName] = groupName
    
        let groupZone = CKRecordZone(zoneName: groupName)
    
        let shareID = CKRecord.ID(recordName: groupName, zoneID: groupZone)
    
        let share = CKShare(rootRecord: groupRecord, shareID: shareID)
    
        share.publicPermission = .readWrite
    }
    

    The recordName should be something guaranteed to be unique though - otherwise you can potentially run into problems.