in Sharing/ViewModels/ViewModel.swift [161:189]
func fetchOrCreateShare(contact: Contact, completionHandler: @escaping (Result<(CKShare, CKContainer), Error>) -> Void) {
guard let existingShare = contact.associatedRecord.share else {
let share = CKShare(rootRecord: contact.associatedRecord)
share[CKShare.SystemFieldKey.title] = "Contact: \(contact.name)"
let operation = CKModifyRecordsOperation(recordsToSave: [contact.associatedRecord, share])
operation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) in
if let error = error {
completionHandler(.failure(error))
debugPrint("Error saving CKShare: \(error)")
} else {
completionHandler(.success((share, self.container)))
}
}
database.add(operation)
return
}
database.fetch(withRecordID: existingShare.recordID) { (share, error) in
if let error = error {
completionHandler(.failure(error))
} else if let share = share as? CKShare {
completionHandler(.success((share, self.container)))
} else {
completionHandler(.failure(ViewModelError.invalidRemoteShare))
}
}
}