CloudKit: How to use CKFetchShareParticipants and CKModifyRecords to get share URL?

381 views Asked by At

I'm following this WWDC video and some sample code found online (regrettably Apple didn't include sample code in its WWDC talk), with this code below I am getting my CKShareParticipant ok but this block modOperation.modifyRecordsCompletionBlock is not returning at all, what am I doing wrong?

 private func share(record: CKRecord) {
        
        let share = CKShare(rootRecord: record)
        // save
        let operation = CKFetchShareParticipantsOperation(userIdentityLookupInfos: [CKUserIdentity.LookupInfo(emailAddress: "[email protected]")])
        
        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
    }
1

There are 1 answers

0
GarySabo On BEST ANSWER

This ended up just being a typo, I just needed to delete this line operation.shareParticipantFetchedBlock = { participant in under the declaration of modOperation