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

时间:2017-09-11 05:41:16

标签: swift database cloudkit ckrecord

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

以下是有问题的方法。它连接到NSWindow中的按钮的简单IBAction。您可以忽略我从模型数据创建CKRecords的嵌套循环。他们似乎按预期工作。我的错误检查代码位于CKModifyRecordsOperation的完成块内。具体来说,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())")
//            }
//        }
    }

1 个答案:

答案 0 :(得分:1)

重复记录的问题是您重复使用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")