CloudKit:如何使用 CKFetchShareParticipants 和 CKModifyRecords 获取分享 URL?

Posted

技术标签:

【中文标题】CloudKit:如何使用 CKFetchShareParticipants 和 CKModifyRecords 获取分享 URL?【英文标题】:CloudKit: How to use CKFetchShareParticipants and CKModifyRecords to get share URL? 【发布时间】:2021-05-13 18:55:28 【问题描述】:

我正在关注这个 WWDC video 和一些在线找到的示例代码(遗憾的是,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
    

【问题讨论】:

【参考方案1】:

这只是一个错字,我只需要在modOperation的声明下删除这行 operation.shareParticipantFetchedBlock = participant in

【讨论】:

以上是关于CloudKit:如何使用 CKFetchShareParticipants 和 CKModifyRecords 获取分享 URL?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Cloudkit 快速从 iCloud 获取当前用户信息

如何使用 CloudKit 查询条件 segues?

如何使用查询快速获取 cloudkit 数据

如何使用cloudkit设置“报告不当内容”?

CloudKit:如何使用 CKFetchShareParticipants 和 CKModifyRecords 获取分享 URL?

如何通过 npm 安装 CloudKit JS 库