CloudKit - CKRecord.ID 可接受的记录名称?
CloudKit - CKRecord.ID acceptable recordName?
我已经编写了一种在自定义区域中保存记录的方法,它似乎按预期工作。但是,我不确定的一件事是 CKRecord.ID
recordName
。现在我只使用 UUID
字符串。是否有分配 recordName
的首选方法?当前 CloudKit 示例非常稀少,而且 CK 文档的相当一部分似乎已经过时。谢谢。
func saveToCloud(record: String, recordType: String, recordTypeField: String, reference: CKRecord?, referenceType: String?) {
let zoneID = CKRecordZone.ID(zoneName: Zone.test, ownerName: CKRecordZone.ID.default.ownerName)
let recordID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
let newRecord = CKRecord(recordType: recordType, recordID: recordID)
if let reference = reference, let referenceType = referenceType {
let newReference = CKRecord.Reference(record: reference, action: .none)
newRecord[referenceType] = newReference
}
newRecord[recordTypeField] = record
database.save(newRecord) { (_,error) in
if let err = error as? CKError {
print("ERROR =" , err.userInfo )
}
}
}
从我的角度来看,它在 CKRecord.ID 的文档中非常清楚:
A record ID object consists of a name string and a zone ID. The name string is an ASCII string not exceeding 255 characters in length. For automatically created records, the ID name string is based on a UUID and is therefore guaranteed to be unique. When creating your own record ID objects, you are free to use names that have more meaning to your app or to the user, as long as each name is unique within the specified zone. For example, you might use a document name for the name string.
根据我的经验,最好从记录键生成记录名称,即从必须唯一的字段组合生成记录名称。
这样即使您错误地尝试添加相同的记录两次,您也可以保证唯一性。
据我所知,没有其他方法可以使某些字段组合在 CloudKit 中唯一。
我同意 UUID 是理想的。示例:
let newId = CKRecord.ID(recordName: UUID().uuidString)
但是,重要的是不要尝试使用无效字符或零长度字符串创建一个。结果是致命错误!因此,当从不受信任的来源加载它们时(比如从服务器输出解析),我使用如下安全功能:
extension CKRecord.ID {
public static func fromUntrusted(_ string: String?) -> CKRecord.ID? {
guard let string = string else { return nil }
guard let _ = string.data(using: .ascii, allowLossyConversion: false) else { return nil }
guard string.count > 0 && string.count < 255 else { return nil }
return CKRecord.ID(recordName: string)
}
}
用法:
if let recordId = CKRecord.ID.fromUntrusted(response) {
// use value
}
我已经编写了一种在自定义区域中保存记录的方法,它似乎按预期工作。但是,我不确定的一件事是 CKRecord.ID
recordName
。现在我只使用 UUID
字符串。是否有分配 recordName
的首选方法?当前 CloudKit 示例非常稀少,而且 CK 文档的相当一部分似乎已经过时。谢谢。
func saveToCloud(record: String, recordType: String, recordTypeField: String, reference: CKRecord?, referenceType: String?) {
let zoneID = CKRecordZone.ID(zoneName: Zone.test, ownerName: CKRecordZone.ID.default.ownerName)
let recordID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
let newRecord = CKRecord(recordType: recordType, recordID: recordID)
if let reference = reference, let referenceType = referenceType {
let newReference = CKRecord.Reference(record: reference, action: .none)
newRecord[referenceType] = newReference
}
newRecord[recordTypeField] = record
database.save(newRecord) { (_,error) in
if let err = error as? CKError {
print("ERROR =" , err.userInfo )
}
}
}
从我的角度来看,它在 CKRecord.ID 的文档中非常清楚:
A record ID object consists of a name string and a zone ID. The name string is an ASCII string not exceeding 255 characters in length. For automatically created records, the ID name string is based on a UUID and is therefore guaranteed to be unique. When creating your own record ID objects, you are free to use names that have more meaning to your app or to the user, as long as each name is unique within the specified zone. For example, you might use a document name for the name string.
根据我的经验,最好从记录键生成记录名称,即从必须唯一的字段组合生成记录名称。
这样即使您错误地尝试添加相同的记录两次,您也可以保证唯一性。
据我所知,没有其他方法可以使某些字段组合在 CloudKit 中唯一。
我同意 UUID 是理想的。示例:
let newId = CKRecord.ID(recordName: UUID().uuidString)
但是,重要的是不要尝试使用无效字符或零长度字符串创建一个。结果是致命错误!因此,当从不受信任的来源加载它们时(比如从服务器输出解析),我使用如下安全功能:
extension CKRecord.ID {
public static func fromUntrusted(_ string: String?) -> CKRecord.ID? {
guard let string = string else { return nil }
guard let _ = string.data(using: .ascii, allowLossyConversion: false) else { return nil }
guard string.count > 0 && string.count < 255 else { return nil }
return CKRecord.ID(recordName: string)
}
}
用法:
if let recordId = CKRecord.ID.fromUntrusted(response) {
// use value
}