Swift cloudkit CKFetchRecordZoneChangesOptions 无法返回删除记录

Posted

技术标签:

【中文标题】Swift cloudkit CKFetchRecordZoneChangesOptions 无法返回删除记录【英文标题】:Swift cloudkit CKFetchRecordZoneChangesOptions can't return delete record 【发布时间】:2017-09-01 04:24:54 【问题描述】:

我不知道为什么我在CKFetchRecordZoneChangesOptions 上无法获取任何删除记录,我可以获取创建和修改的记录,但delete 就是无法删除记录。 昨天,我可以正常获取删除更改记录。

这是我的代码

public func handleNotification(whichNotification : Int) 

    zoneID = UserDataCustomZone.zoneID

    if #available(ios 10.0, *) 

        // Use the ChangeToken to fetch only whatever changes have occurred since the last
        // time we asked, since intermediate push notifications might have been dropped.
        var changeToken: CKServerChangeToken? = nil
        let changeTokenData = UserDefaults.standard.data(forKey: serverChangeTokenKey)

        if changeTokenData != nil 
            changeToken = NSKeyedUnarchiver.unarchiveObject(with: changeTokenData!) as! CKServerChangeToken?
        

        let options = CKFetchRecordZoneChangesOptions()
        options.previousServerChangeToken = changeToken
        let optionsMap = [zoneID!: options]
        let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [zoneID!], optionsByRecordZoneID: optionsMap)
        operation.fetchAllChanges = true
        // only can receive created and modified change record
        operation.recordChangedBlock =  record in
           //show record
            print("check this out \(record)")

            

    

    operation.recordZoneChangeTokensUpdatedBlock =  zoneID, changeToken, data in

        print("print Token \(String(describing: changeToken))")

        guard let changeToken = changeToken else 
            return
        

        let changeTokenData = NSKeyedArchiver.archivedData(withRootObject: changeToken)
        UserDefaults.standard.set(changeTokenData, forKey: self.serverChangeTokenKey)
    

    operation.recordZoneFetchCompletionBlock =  zoneID, changeToken, data, more, error in
        guard error == nil else 
            return
        
        guard let changeToken = changeToken else 
            return
        

        let changeTokenData = NSKeyedArchiver.archivedData(withRootObject: changeToken)
        UserDefaults.standard.set(changeTokenData, forKey: self.serverChangeTokenKey)
    

    operation.fetchRecordZoneChangesCompletionBlock =  error in
        guard error == nil else 
            return
        
    

    operation.qualityOfService = .utility

    let container = CKContainer.default()
    let db = container.privateCloudDatabase
    db.add(operation)

      else 
        // Fallback on earlier versions
    


【问题讨论】:

当我删除 Cloudkit 上的数据时,我可以获得订阅远程通知。 【参考方案1】:

我注意到您已经定义了一个recordZoneChangeTokensUpdatedBlock 处理程序,它足以接收记录的创建和更新。但是,CKFetchRecordZoneChangesOperation 通过其recordWithIDWasDeletedBlock 处理程序通知记录删除。声明是:

var recordWithIDWasDeletedBlock: ((CKRecordID, String) -> Void)? get set

所以,基本上你需要包含以下额外的行:

operation.recordWithIDWasDeletedBlock = recordID, recordType in ...

在您的原始代码中。

参考Apple developer documentation。

整个过程和参考代码可以在CloudKit Quick Start指南的Fetching Changes章节Maintaining a Local Cache of CloudKit Records找到。

【讨论】:

以上是关于Swift cloudkit CKFetchRecordZoneChangesOptions 无法返回删除记录的主要内容,如果未能解决你的问题,请参考以下文章

Swift / Cloudkit 谓词格式

swift Swift - CloudKit - 删除订阅

swift Swift - CloudKit - 谓词来获取用户

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享