我正在关注这个WWDC 视频和一些在线找到的示例代码(遗憾的是,Apple 没有在其 WWDC 演讲中包含示例代码),下面的代码我没问题,CKShareParticipant
但是这个块modOperation.modifyRecordsCompletionBlock
根本没有返回,我在做什么错误的?
private func share(record: CKRecord) {
let share = CKShare(rootRecord: record)
// save
let operation = CKFetchShareParticipantsOperation(userIdentityLookupInfos: [CKUserIdentity.LookupInfo(emailAddress: "friendsEmail@gmail.com")])
var participants = [CKShare.Participant]()
// Collect the participants as CloudKit generates them.
operation.shareParticipantFetchedBlock = { participant in
participants.append(participant)
}
// If the operation fails, return the error to the caller.
// Otherwise, return the array of participants.
operation.fetchShareParticipantsCompletionBlock = { error in
if let error = error {
print("error: ", error)
} else {
for participant in participants {
print("we have a participant! = \(participant)")
let modOperation: CKModifyRecordsOperation = CKModifyRecordsOperation(recordsToSave: [record, share], recordIDsToDelete: nil)
operation.shareParticipantFetchedBlock = { participant in
participant.permission = .readOnly
share.addParticipant(participant)
modOperation.savePolicy = .ifServerRecordUnchanged
//nothing in this block gets called
modOperation.modifyRecordsCompletionBlock = {records, recordIDs, error in
if let error = error {
print("error in modifying the records: ", error)
} else {
print("TESTING records = \(records) recordIDs = \(recordIDs)")
}
}
}
modOperation.qualityOfService = .userInitiated
container.privateCloudDatabase.add(modOperation)
} //end of for participant in participants
}
} //end of operation.fetchShareParticipantsCompletionBlock
// Set an appropriate QoS and add the operation to the
// container's queue to execute it.
operation.qualityOfService = .userInitiated
container.add(operation) //It was important to make sure this is the same container
}