按记录名称获取多条记录
Posted
技术标签:
【中文标题】按记录名称获取多条记录【英文标题】:Fetching Multiple Records by Record Name 【发布时间】:2017-02-20 07:35:17 【问题描述】:我有一个记录名称(或字符串)数组。我想在公共数据库中获取任何具有这些记录名称的记录。我该怎么做?
【问题讨论】:
【参考方案1】:你应该应用这样的东西......
let filter: [String] = [ "String1", "String2", "String3" ]
let predicate: NSPredicate = NSPredicate(format: "%k IN %@", "record_name", filter)
let query: CKQuery = CKQuery(recordType: "RecordType", predicate: predicate)
CKContainer.default.publicCloudDatabase.perform(query, inZoneWith: nil, completionHandler: @escaping ([CKRecord]?, Error?) -> Void in
// Do what you want with your filtered CKRecords ;)
)
NSPredicate
使用聚合运算符IN
来过滤数组的内容。更多信息Predicate Programming Guide
【讨论】:
【参考方案2】:查看CKFetchRecordsOperation(recordIDs: [CKRecord.ID])
您没有包含代码示例,但如果您将 recordID 作为字符串数组,则:
let ids: [String] = [...] // your array of the record names as Strings
let recordIDs = ids.map CKRecord.ID(recordName: $0)
let operation = CKFetchRecordsOperation(recordIDs: recordIDs)
operation.fetchRecordsCompletionBlock = (ckRecords, error) in
... // do stuff with your records
CKContainer.default().publicCloudDatabase.add(operation)
【讨论】:
能否请您举例说明如何处理您的记录?我尝试let recordField1 = ckRecords["recordField1"]
访问它们,但收到错误No exact matches in call to subscript
。感谢您的帮助!
你具体想做什么?我认为您的问题超出了原始问题的范围。但请注意,ckRecords 是一本字典。 developer.apple.com/documentation/cloudkit/…以上是关于按记录名称获取多条记录的主要内容,如果未能解决你的问题,请参考以下文章