gpt4 book ai didi

swift - 为什么 CloudKit 不允许我将任何记录保存到默认容器的公共(public)数据库中?

转载 作者:行者123 更新时间:2023-11-28 15:20:08 25 4
gpt4 key购买 nike

我有一个 CKRecords 数组,这是我第一次尝试将其保存到默认 Container 的公共(public)数据库中。我创建了一个 CKModifyRecordsOperation 并尝试将操作添加到公共(public)数据库。它不成功,但我没有收到任何错误消息。所以我尝试将每个方法单独添加到数据库中,没有任何 CKOperation ... 也不成功,也没有错误消息。可以肯定的是,我检查了我的 CloudKit 仪表板;那里什么都没有。

下面是有问题的方法。它连接到 NSWindow 中按钮的简单 IBAction。您可以忽略我从模型数据创建 CKRecords 的嵌套循环。他们似乎按预期工作。我的错误检查代码位于 CKModifyRecordsOperation 的完成 block 内。具体来说,modifyRecordsCompletionBlock 正在打印:“将 0 条记录保存到 CloudKit 中。”

    @IBAction func uploadDataToCloudKit(_ sender: NSButton) {
// Get CloudKit container and Public Database
let myContainer = CKContainer.default()
let publicDB = myContainer.publicCloudDatabase

// Create array of cloudkit records so we can save a batch
var cloudRecords = [CKRecord]()

let groups = PayCodes().groups

// For each PayCode Group...
for payCodeGroup in groups {
// Create CKRecord and give it a type
let payCodeGroupRecord = CKRecord(recordType: "payCodeData")

// For each PayCode in the group...
for payCode in payCodeGroup.payCodes {
// Fill the new CKRecord with key/value pairs
payCodeGroupRecord.setObject(payCodeGroup.title as NSString, forKey: "group")

if let commercialID = payCode.id.commercial as NSString?, let residentialID = payCode.id.residential as NSString? {
payCodeGroupRecord.setObject(commercialID, forKey: "commercialID")
payCodeGroupRecord.setObject(residentialID, forKey: "residentialID")
}

payCodeGroupRecord.setObject(payCode.title as NSString, forKey: "title")

if let description = payCode.description as NSString? {
payCodeGroupRecord.setObject(description, forKey: "description")
}

payCodeGroupRecord.setObject((payCode.includesTripCharge ? 1 : 0) as NSNumber, forKey: "includesTripCharge")
payCodeGroupRecord.setObject((payCode.needsResolutionCode ? 1 : 0) as NSNumber, forKey: "needsResolutionCode")


payCodeGroupRecord.setObject(payCode.pay.commercial as NSNumber, forKey: "commercialPay")
payCodeGroupRecord.setObject(payCode.pay.residential as NSNumber, forKey: "residentialPay")

// Save new CKRecord into the Array
cloudRecords.append(payCodeGroupRecord)
}
}

print("There are \(cloudRecords.count) records about to be saved...")
// print(cloudRecords)
// Create an operation to save the records
let operation = CKModifyRecordsOperation(recordsToSave: cloudRecords, recordIDsToDelete: nil)

operation.perRecordCompletionBlock = { savedRecord, error in
guard error != nil else {
print("There was an error: \(error!.localizedDescription)")
return
}

print("Saved a record into CloudKit")
}

operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
guard error != nil else {
print("There was an error: \(error!.localizedDescription)")
return
}

guard let savedRecords = savedRecords else { return }
print("Saved \(savedRecords.count) records into CloudKit")
}

// Run the operation
publicDB.add(operation)
// cloudRecords.forEach { record in
// publicDB.save(record) { savedRecord, error in
// guard error != nil else {
// print("There was an error: \(error!.localizedDescription)")
// return
// }
//
// guard let savedRecord = savedRecord else { return }
// print("Saved a record: \(savedRecord.allKeys())")
// }
// }
}

最佳答案

重复记录的问题是您重用了 CKRecord 实例。

改变:

for payCodeGroup in groups {
// Create CKRecord and give it a type
let payCodeGroupRecord = CKRecord(recordType: "payCodeData")

// For each PayCode in the group...
for payCode in payCodeGroup.payCodes {
// Fill the new CKRecord with key/value pairs
payCodeGroupRecord.setObject(payCodeGroup.title as NSString, forKey: "group")

到:

for payCodeGroup in groups {
// For each PayCode in the group...
for payCode in payCodeGroup.payCodes {
// Create CKRecord and give it a type
let payCodeGroupRecord = CKRecord(recordType: "payCodeData")

// Fill the new CKRecord with key/value pairs
payCodeGroupRecord.setObject(payCodeGroup.title as NSString, forKey: "group")

关于swift - 为什么 CloudKit 不允许我将任何记录保存到默认容器的公共(public)数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46148870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com