I watched last WWDC 2016 What's New with CloudKit to understand how to share records with other users using CKShare
Single record sharing:
I am able to share and retrieve a single record
i.e if [email protected] has created and shared a single record to [email protected]
Multiple records sharing:
let's say there are 10 records and [email protected] wants to share to [email protected]. I am facing the issue when user [email protected] shares multiple records to user [email protected]
What I have tried so far:
First I created 3 note records:
Note1
Note2 ( set parent as Note1 )
Note3 ( set parent as Note1 )
I shared Note1 ( Parent record ) with below code:
CODE - Share record
let controller = UICloudSharingController { controller,
preparationCompletionHandler in
let share = CKShare(rootRecord: self.parentRecord!)
share[CKShareTitleKey] = "note" as CKRecordValue
share.publicPermission = .readOnly
let operation = CKModifyRecordsOperation(
recordsToSave: [self.parentRecord!, share],
recordIDsToDelete: nil)
operation.perRecordCompletionBlock = { (record, error) in
if let error = error {
print("CloudKit error: \(error.localizedDescription)")
}
}
operation.modifyRecordsCompletionBlock = { records, recordIDs, error in
if error != nil {
print(error?.localizedDescription ?? "Error")
} else{
print("Success")
preparationCompletionHandler(share,CKContainer.default(), error)
}
}
CKContainer.default().privateCloudDatabase.add(operation)
}
controller.availablePermissions = [.allowPrivate, .allowReadOnly]
controller.delegate = self
present(controller, animated: true)
and retrieved shared-note with below code:
CODE - Read data from shared-note
func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {
let acceptSharesOperation = CKAcceptSharesOperation(
shareMetadatas: [cloudKitShareMetadata])
acceptSharesOperation.perShareCompletionBlock = {
metadata, share, error in
if error != nil {
print(error?.localizedDescription)
} else {
let operation = CKFetchRecordsOperation(
recordIDs: [cloudKitShareMetadata.rootRecordID])
operation.perRecordCompletionBlock = { record, _, error in
if error != nil {
print(error?.localizedDescription)
}
if let shareRecord = record {
DispatchQueue.main.async() {
// Shared record successfully fetched. Update user
// interface here to present to user.
print("\(shareRecord["id"]!)") // id of note
print("\(shareRecord["text"])") // text of note
print("Shared record successfully fetched")
}
}
}
operation.fetchRecordsCompletionBlock = { (recordsWithRecordIDs,error) in
if error != nil {
print(error?.localizedDescription)
}else {
if let recordsWithRecordIDs = recordsWithRecordIDs {
print("Count \(recordsWithRecordIDs.count)")
}
}
}
CKContainer.default().sharedCloudDatabase.add(operation)
}
}
CKContainer(identifier: cloudKitShareMetadata.containerIdentifier)
.add(acceptSharesOperation)
}
Above method gives only parent note data ( root/parent only )
Queries:
1) How to fetch other children note records? ( I used term children for understanding purpose )
2) Do I need to check every time whether there are new shared records or not
As I haven't found any good tutorials and the source from official Apple docs. Would you suggest me the approach to share and retrieve multiple records?
Thanks in advance..!