我正在尝试将CKReference添加到云工具包中的记录中,但尝试会一直触发"服务记录更改".从我的println显示的控制台消息(控制台消息和代码如下),我上传了带有0个引用的记录,然后当我附加引用时,我看到尝试上传带有1个引用的记录.然后我收到了错误.
据我了解,不应触发"服务记录已更改",因为参考列表中的值已更改(记录有一个完整的额外字段).即使我处于开发模式,我手动为参考列表创建了键值字段,因为当引用列表为空时,第一个记录上载不包括该字段(上传空数组会导致另一个错误).
我将在控制台消息之后按相关性顺序包含代码(您将能够看到大多数println).整个项目在github上,如果需要,我可以链接到它或包含更多代码.
相关控制台:
name was set
uploading TestCrewParticipant
with 0 references
if let projects
upload succeeded: TestCrewParticipant
attaching reference
adding TestVoyage:_d147aa657fbf2adda0c82bf30d0e29a9 from guard
references #: Optional(1)
uploading TestCrewParticipant
with 1 references
if let projects
success: 1
uploading TestCrewParticipant
with 1 references
if let projects
success: 1
local storage tested: TestCrewParticipant
u!error for TestCrewParticipant
CKError: <CKError 0x7fcbd05fa960: "Server Record Changed" (14/2004); server message = "record to insert already exists"; uuid = 96377029-341E-487C-85C3-E18ADE1119DF; container ID = "iCloud.com.lingotech.cloudVoyageDataModel">
u!error …Run Code Online (Sandbox Code Playgroud) 当我使用保存策略保存包含CKAsset(只是 JPG 图像)的记录时,我正在测试同步冲突。我收到错误。这会返回用于解决冲突的有用信息,包括我尝试保存的信息以及记录的当前服务器版本。第一个在,第二个在。CKModifyRecordsOperation.IfServerRecordUnchangedCKErrorCode.ServerRecordChangedCKErrorCKRecorderror.userInfo[CKRecordChangedErrorClientRecordKey]error.userInfo[CKRecordChangedErrorServerRecordKey]
我的问题是我尝试使用以下代码访问服务器记录的 CKAsset:
if let photoAsset = rec["myPhoto"] as? CKAsset {
print("PhotoAsset.fileURL: \(photoAsset.fileURL)") // BAD_ACCESS ERROR HERE
self.myPartner.photo = NSData(contentsOfURL: photoAsset.fileURL)
}
Run Code Online (Sandbox Code Playgroud)
我不明白这怎么可能。但经过进一步调查,我打印出了客户端和服务器 CKRecords,而服务器缺少“路径”属性。
客户端 CKAsset...myPhoto (已修改)-> CKAsset:0x7b960d90;路径=~/tmp/BF185B2C-7A39-4730-9530-9797E843243A照片,大小=373959,uploadRank=0,uploadReceipt=A92Eg1qoyPG7yrg3,UUID=3C2D5DC8-4FF5-4A81-853B-395FC1C59862,referenceSignature=<012 fd149 200fc600 617e3907 88763e3e 5002abbf 5b> 、flags=已上传、wrappedEncryptionKey=、签名=<0134a297 38d52f5f 9275bfba fce5b1a8 3d6b9692 d3>
服务器 CKAsset...myPhoto = CKAsset: 0x7be700d0; ReferenceSignature=<015337bd 84409893 7c014f46 36248d27 ce911dc3 7a>,大小=373959,uploadRank=0,UUID=DF5D2EB4-033C-49A2-AF52-6055B5A44106,wrappedEncryptionKey=<767e7cfd d1e62 110 32119ee9 f6f026b3 5bcf0cc3 8053a4de>,签名=<0134a297 38d52f5f 9275bfba fce5b1a8 3d6b9692 d3>
请注意,path=~/tmp/C706423B-A3E8-4051-A9B3-483C718BFBF5photo服务器中缺少什么?谁能解释一下吗?为了解决这个问题,我尝试避免接触服务器记录中的 CKAsset。我希望至少能够检查是否为零。我想把它放在那里以防对其他人有帮助。
CKError在应用程序中向用户显示遇到的警报.注意:此问题与要显示的UI代码无关.只想从错误中提取有意义的字符串.
我尝试使用localizedDescription但它似乎没有包含适当的字符串
以下是我的尝试:
po error
<CKError 0x1c464cea0: "Network Unavailable" (3/NSURLErrorDomain:-1009); "The Internet connection appears to be offline.">
po error.localizedDescription
"The operation couldn’t be completed. (CKErrorDomain error 3.)"
po (error as! CKError).errorUserInfo
? 2 elements
? 0 : 2 elements
- key : "NSUnderlyingError"
- value : Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https:/
? 1 : 2 elements
- key : "NSDebugDescription"
- value : NSURLErrorDomain: -1009
po (error …Run Code Online (Sandbox Code Playgroud) 我正在使用 CloudKit 并且正在检查是否已经创建了特定区域。
对于这个例子,假设一个区域没有设置,所以 CloudKit 检索我一个CKError.
这CKError有一个名为partialErrorsByItemIDwhich 类型的属性[AnyHashable : Error]?
这是代码:
fileprivate func checkIfZonesWereCreated() {
let privateDB = CKContainer.default().privateCloudDatabase
let op = CKFetchRecordZonesOperation(recordZoneIDs: [zoneID1, zoneID2])
op.fetchRecordZonesCompletionBlock = { (dict, err) in
if let err = err as? CKError, let _err = err.partialErrorsByItemID {
print(_err)
/* prints
[AnyHashable(<CKRecordZoneID: 0x60800003cba0; ownerName=__defaultOwner__, zoneName=TestZone>): <CKError 0x60400005a760: "Zone Not Found" (26/2036); server message = "Zone 'TestZone' does not exist"; uuid = ...-2DF4E13F81E2; container ID = "iCloud.com.someContainer">]
*/
// …Run Code Online (Sandbox Code Playgroud) 获取后.partialFailure CKError,我一直在尝试恢复ID和相应的错误,但是我遇到了问题...
现在我正在使用:
print("pE \(error.partialErrorsByItemID) or \(error.userInfo[CKPartialErrorsByItemIDKey])")
if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [NSObject: Error] {
print("partialErrors #\(dictionary.count)") // <-- Not reaching this...
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下方法:
if let dictionary = error.partialErrorsByItemID { // <-- error.pEBIID returns nil
Run Code Online (Sandbox Code Playgroud)
和:
if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [CKRecord : CKError /* and Error */] { // <-- but neither triggers the if-let
Run Code Online (Sandbox Code Playgroud)
第一次打印在控制台中显示了这一点(我将打开的标签向左切换,以便它们不会被解释为html):
pE nil or Optional({
">CKRecordID: 0x7b95ace0; CentralTableView:(_defaultZone:__defaultOwner__)>" = ">CKError 0x7a7e4cf0: \"Server Record Changed\" (14/2004); server message = \"record to insert …Run Code Online (Sandbox Code Playgroud)