I've already played with the samples and was able to share one by one or a set of records. BUT, let me rephrase, I have a set of entities that I want to share ONCE between invited users with ONE sharing at the top. Could you confirm that is it possible?
If Yes, there is probably one missing step or misunderstood in the following code. In my understanding, based on the samples, to do this, I'm doing :
1/ on owner and participant : configure a NSPersistentCloudKitContainer with inside a databaseScope = .private store AND a databaseScope = .shared store
2/ on owner before presenting the UICloudSharingController:
2.1/ create a shared zone-wide share ID
let shareRecordID = CKRecord.ID(recordName: CKRecordNameZoneWideShare, zoneID: zoneID)
2.2/ create or get an existing CKShare with:
func getOrCreateShare() async throws -> CKShare {
let zoneID = CKRecordZone.ID(zoneName: sharedZoneName)
// Zone-wide share ID
let shareRecordID = CKRecord.ID(
recordName: CKRecordNameZoneWideShare,
zoneID: zoneID
)
// Try to fetch existing share
do {
let existingShare = try await privateDatabase.record(for: shareRecordID) as? CKShare
if let share = existingShare {
print("Existing share retrieved")
return share
}
} catch let error as CKError where error.code == .unknownItem {
print("No existing share, creating new one for the entire zone")
let share = CKShare(recordZoneID: zoneID)
share[CKShare.SystemFieldKey.title] = "My Sharing Title" as CKRecordValue
share[CKShare.SystemFieldKey.shareType] = "net.megy.stokk.inventory" as CKRecordValue
share.publicPermission = .none // Private only
guard let savedShare = try await privateDatabase.save(share) as? CKShare else {
throw CKError(.unknownItem)
}
print("Return new share created for entire zone")
return savedShare
}
// should not be reached
throw CKError(.unknownItem)
}
3/ on the participant side, that I've plugged my SceneDelegate windowScene(_ windowScene:userDidAcceptCloudKitShareWith:) to accep the invitation with:
public func acceptShare(metadata: CKShare.Metadata) async throws {
try await CKContainer(identifier: cloudKitIdentifier).accept(metadata)
print("Invitation accepted, syncing...")
}