ios - 如何在自定义区域中创建 CKRecord 而无需指定记录名称?

标签 ios swift cloudkit

我使用以下代码在自定义私有(private) CKRecordZone 中创建 CKRecord:

let noteZoneID = CKRecordZone.ID(zoneName: CloudKitManager.noteZoneName,
                                 ownerName: CKCurrentUserDefaultName)
return CKRecord(recordType: Note.recordType, zoneID: noteZoneID)

这似乎工作得很好,但我收到警告:'init(recordType:zoneID:)' 已弃用:使用 init(recordType:recordID:) + CKRecord.ID(zoneID:) 代替

如果我使用这些方法,我想我必须这样做:

let noteZoneID = CKRecordZone.ID(zoneName: CloudKitManager.noteZoneName,
                                 ownerName: CKCurrentUserDefaultName)
let recordID = CKRecord.ID(recordName: "UniqueRecordName", zoneID: noteZoneID)
return CKRecord(recordType: Note.recordType, recordID: recordID)

我对这种方法的问题是,突然间我负责创建一个唯一的recordName,但我不明白为什么会出现这种情况。是否有其他方法可以创建记录来消除该责任?我的应用代码应该出现 recordName 是否有特定原因?

最佳答案

自定义区域无法绕过此问题。如果您不想分配 recordName 值,可以向其传递 UUID().uuidString

Record ID objects are normally created automatically when you create a new record, but [...] you must create record ID objects when you want to save a record in a zone other than the default zone.

https://developer.apple.com/documentation/cloudkit/ckrecord/id

关于ios - 如何在自定义区域中创建 CKRecord 而无需指定记录名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904084/

相关文章:

ios - iCloud 在初始同步期间正在做什么?

ios - 交互式过渡不是线性的

ios - Swift:通过单击按钮修改数据格式

ios - SWIFT:为什么我没有从 CloudKit 应用程序收到通知?

ios - CloudKit "aggregate"是否更新更改通知?

iphone - 要在 ios 中显示 map ,我们应该使用 ArcGIS Apis 还是 Google Map API?

iphone - 使用来自代码的 JavaScript 在 webview 中设置 HTML 内容。

ios - 快速处理 SWRevealViewController 中的 CellForRowAtIndexPath?

arrays - 从 cloudkit-query 获取结果

ios - 将数据输入到不同类中的不同结构