swift Swift - CloudKit - 获取添加保存删除多个记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit - 获取添加保存删除多个记录相关的知识,希望对你有一定的参考价值。

01. Save and Delete Records at the same time

let recordsToSave: [CKRecord] ...
let recordIDsToDelete: [CKRecordID] ...
let saveRecordsOperation = CKModifyRecordsOperation(
      recordsToSave: recordsToSave,
      recordIDsToDelete: recordIDsToDelete
)
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
    // handle errors here
}
let privateDB = CKContainer.defaultContainer().privateCloudDatabase
privateDB.addOperation(saveRecordsOperation)

02. Fetch and Update Records

var publicDatabase: CKDatabase? = CKContainer.default().publicCloudDatabase
var artworkRecordID = CKRecordID(recordName: "115")
publicDatabase?.fetch(withRecordID: artworkRecordID, completionHandler: {(_ artworkRecord: CKRecord, _ error: Error?) -> Void in
    if error != nil {
        // Error handling for failed fetch from public database
    }
    else {
            // Modify the record and save it to the database
        var date = artworkRecord["date"] as? Date
        artworkRecord["date"] = (date.addingTimeInterval(30.0 * 60.0)) as? CKRecord ?? CKRecord()
        publicDatabase?.save(artworkRecord, completionHandler: {(_ savedRecord: CKRecord, _ saveError: Error?) -> Void in
            // Error handling for failed save to public database
        })
    }
})

以上是关于swift Swift - CloudKit - 获取添加保存删除多个记录的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享

swift Swift - CloudKit - 维护本地缓存

swift Swift - CloudKit - 部署架构

swift Swift - CloudKit订阅 - 5