How can I check if a RecordZone is shared with CloudKit?

340 views Asked by At

I am interested in checking if a RecordZone has been shared. When I share a zone, I know there is a share record created in that zone. In Apple's CloudKit example, it checks to see if a specific record is shared:

func fetchOrCreateShare(contact: Contact) async throws -> (CKShare, CKContainer) {
   guard let existingShare = contact.associatedRecord.share else {
    let share = CKShare(rootRecord:contact.associatedRecord)      
   share[CKShare.SystemFieldKey.title] = "Contact: \.(contact.name)" 
    _ = try await database.modifyRecords(saving:[contact.associatedRecord, share], deleting: []). 
    return (share, container)
  }
  guard let share = try await database.record(for: existingShare) as? CKShare else {
    throw ViewModelError.invalidRemoteShare
  }
  return (share, container)
}

The first line of the method guard let existingShare = contact.associatedRecord.share checks if a specific record is being shared, but in the case of a shared zone, given a zone name, how can I check the .share of that zone? When I try CKRecordZone(zoneName: "Contacts").share, the value is nil even though the zone is being shared.

1

There are 1 answers

0
malhal On

I believe that is explained in the CKShare docs shown in bold below:

After CloudKit saves the share, a participant can fetch its corresponding metadata, which includes a reference to the share, information about the user’s participation, and, for shared hierarchies, the root record’s record ID. Create an instance of CKFetchShareMetadataOperation using the share’s URL and add it to the container’s queue to execute it. The operation returns an instance of CKShare.Metadata for each URL you provide. This is only applicable if you manually process share acceptance. If a user receives the share URL and taps or clicks it, CloudKit automatically processes their participation.

To determine the configuration of a fetched share, inspect the recordName property of its recordID. If the value is CKRecordNameZoneWideShare, the share is managing a shared record zone; otherwise, it’s managing a shared record hierarchy.

let isZoneWide = (metadata.share.recordID.recordName == CKRecordNameZoneWideShare)