CKModifyRecordsOperation不适用于完整批记录

时间:2017-02-26 05:46:16

标签: ios swift cloudkit

据我了解,CKModifyRecordsOperation(recordsToSave:, recordsToDelete:)方法可以同时修改多条记录并删除多条记录。

在我的代码中,recordsToSave是一个包含2 CKRecords的数组。我没有要删除的记录,因此我将recordsToDelete设置为nil。令人困惑的是,recordsToSave[0]似乎没有recordsToSave[1]正确保存到云端。

在粘贴代码之前提供更多上下文:

在我的应用中,有一个"加入"与Feed上的每个帖子相关联的按钮。当用户点击"加入"按钮,发生2次云交易:1)帖子的引用被添加到joinedList类型的[CKReference],以及2)帖子的记录应该增加NUM_PEOPLE属性。基于CloudKit仪表板,云事务#1正在发生,但不是#2。

这是我的代码,省略了不相关的部分:

@IBAction func joinOrLeaveIsClicked(_ sender: Any) {

self.container.fetchUserRecordID() { userRecordID, outerError in
    if outerError == nil {
        self.db.fetch(withRecordID: userRecordID!) { userRecord, innerError in
            if innerError == nil {

                var joinedList: [CKReference]

                if userRecord!.object(forKey: JOINED_LIST) == nil {
                    joinedList = [CKReference]() // init an empty list
                }
                else {
                    joinedList = userRecord!.object(forKey: JOINED_LIST) as! [CKReference]
                }

                let ref = CKReference(recordID: self.post.recordID, action: .none)

                // ... omitted some of the if-else if-else ladder

                // add to list if you haven't joined already
                else if !joinedList.contains(ref) {

                    // modifying user record
                    joinedList.append(ref) // add to list
                    userRecord?[JOINED_LIST] = joinedList as CKRecordValue // associate list with user record

                    // modifying post
                    let oldCount = self.post.object(forKey: NUM_PEOPLE) as! Int
                    self.post[NUM_PEOPLE] = (oldCount + 1) as CKRecordValue

                    let operation = CKModifyRecordsOperation(recordsToSave: [userRecord!, self.post], recordIDsToDelete: nil)

                    self.db.add(operation)

                }

                    // omitted more of the if-else if-else ladder

            else {
                if let error = innerError as? CKError {
                    print(error)
                }
            }
        }
    }

    else {
        if let error = outerError as? CKError {
            print(error)
        }
    }
}

}

修改

这是我根据第一位评论者的请求添加的代码

operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordsIDs, error in
                            if error == nil {
                                DispatchQueue.main.async(execute: {
                                    self.num.text = String(oldCount + 1) // UI update
                                })
                            }
                            else {
                                print(error!)
                            }
                        }

另一个编辑

let operation = CKModifyRecordsOperation(recordsToSave: [userRecord!, self.post], recordIDsToDelete: nil)

                        operation.perRecordCompletionBlock = { record, error in
                            if error != nil {
                                let castedError = error as! NSError
                                print(castedError)
                            }
                        }
                        operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordsIDs, error in
                            if error == nil {
                                DispatchQueue.main.async(execute: {
                                    self.num.text = String(oldCount + 1) // UI update
                                })
                            }
                            else {
                                print(error!)
                            }
                        }

                        self.db.add(operation)

0 个答案:

没有答案