Swift CloudKit SaveRecord“错误保存记录” [英] Swift CloudKit SaveRecord "Error saving record"
问题描述
我正在尝试将记录保存到CloudKit,但出现错误。我在其他地方看到过,这是一个需要了解如何保存的问题,但我无法解决这个问题。
I am trying to save a record to CloudKit but am getting an error. I had seen elsewhere that this was an issue that required knowing how to save but I can't get this to work.
var database:CKDatabase = CKContainer.defaultContainer().publicCloudDatabase
var aRecord:CKRecord!
if self.cloudId == nil {
var recordId:CKRecordID = CKRecordID(recordName: "RecordId")
self.cloudId = recordId // Setup at top
}
aRecord = CKRecord(recordType: "RecordType", recordID: self.cloudId)
aRecord.setObject(self.localId, forKey: "localId")
// Set the normal names etc
aRecord.setObject(self.name, forKey: "name")
var ops:CKModifyRecordsOperation = CKModifyRecordsOperation()
ops.savePolicy = CKRecordSavePolicy.IfServerRecordUnchanged
database.addOperation(ops)
database.saveRecord(aRecord, completionHandler: { (record, error) in
if error != nil {
println("There was an error \(error.description)!")
} else {
var theRecord:CKRecord = record as CKRecord
self.cloudId = theRecord.recordID
}
})
此gi为我提供错误:
There was an error <CKError 0x16d963e0: "Server Record Changed" (14/2017); "Error saving record <CKRecordID: 0x15651730; xxxxxx:(_defaultZone:__defaultOwner__)> to server: (null)"; uuid = 369226C6-3FAF-418D-A346-49071D3DD70A; container ID = "iCloud.com.xxxxx.xxxx-2">!
不确定,因为我添加了CKModifyRecordsOperation。遗憾的是,Apple文档中没有任何示例。我很想念(您在MSDN上获得的)。
Not sure, given that I have added CKModifyRecordsOperation. Sadly there is no examples within Apple's documentation. I miss that (which you get on MSDN).
感谢偷看!
推荐答案
可以使用CKDatabase的便捷方法 saveRecord:
或通过CKModifyRecordsOperation将记录保存到iCloud。如果是单个记录,则可以使用 saveRecord:
,但需要使用 fetchRecordWithID:$ c来获取要修改的记录。 $ c>,然后再将其保存回iCloud。否则,它只会让您使用新的RecordID保存记录。 在此处更多。
A record can be saved to iCloud using CKDatabase's convenience method saveRecord:
or via a CKModifyRecordsOperation. If it's a single record, you can use saveRecord:
but will need to fetch the record you'd like to modify using fetchRecordWithID:
prior to saving it back to iCloud. Otherwise, it will only let you save a record with a new RecordID. More here.
database.fetchRecordWithID(recordId, completionHandler: { record, error in
if let fetchError = error {
println("An error occurred in \(fetchError)")
} else {
// Modify the record
record.setObject(newName, forKey: "name")
}
}
database.saveRecord(aRecord, completionHandler: { record, error in
if let saveError = error {
println("An error occurred in \(saveError)")
} else {
// Saved record
}
}
上面的代码仅在方向上正确,但不能按原样工作,因为到fetchRecordWithID的完成处理程序返回时,将已经触发saveRecord。一个简单的解决方案是将saveRecord嵌套在完成中fetchRecordWithID的ndler。可能更好的解决方案是将每个调用包装在 NSBlockOperation
中,然后将其添加到NSOperationQueue中,而saveOperation取决于fetchOperation。
The code above is only directionally correct but won't work as is because by the time the completionHandler of fetchRecordWithID returns, saveRecord will have fired already. A simple solution would be to nest saveRecord in the completionHandler of fetchRecordWithID. A probably better solution would be to wrap each call in a NSBlockOperation
and add them to an NSOperationQueue with saveOperation dependent on fetchOperation.
这部分代码用于 CKModifyRecordsOperation
,如果只更新单个记录,则不需要此代码。
This part of your code would be for a CKModifyRecordsOperation
and not needed in case you are only updating a single record:
var ops:CKModifyRecordsOperation = CKModifyRecordsOperation()
ops.savePolicy = CKRecordSavePolicy.IfServerRecordUnchanged
database.addOperation(ops)
如果您确实使用 CKModifyRecordsOperation
,则还需要设置为至少一个完成块,并在与现有记录检测到冲突时处理错误:
If you do use a CKModifyRecordsOperation
instead, you'll also need to set at least one completion block and deal with errors when conflicts are detected with existing records:
let saveRecordsOperation = CKModifyRecordsOperation()
var ckRecordsArray = [CKRecord]()
// set values to ckRecordsArray
saveRecordsOperation.recordsToSave = ckRecordsArray
saveRecordsOperation.savePolicy = .IfServerRecordUnchanged
saveRecordsOperation.perRecordCompletionBlock { record, error in
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
saveRecordsOperation.modifyRecordsCompletionBlock { savedRecords, deletedRecordIDs, error in
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
database.addOperation(saveRecordsOperation)
除了CloudKitAtlas演示应用程序外,没有多少示例代码在Objective-C中。希望这会有所帮助。
There isn't much sample code yet besides the CloudKitAtlas demo app, which is in Objective-C. Hope this helps.
这篇关于Swift CloudKit SaveRecord“错误保存记录”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!