按记录名称获取多条记录

Fetching Multiple Records by Record Name

我有一组记录名称(或字符串)。我想获取 public 数据库中具有任何这些记录名称的任何记录。我该怎么做?

你应该像这样申请...

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

查看CKFetchRecordsOperation(recordIDs: [CKRecord.ID])

您没有包含代码示例,但如果您将 recordID 作为字符串数组,则:

let ids: [String] = [...] // your array of the record names as Strings 
let recordIDs = ids.map { CKRecord.ID(recordName: [=10=]) }
let operation = CKFetchRecordsOperation(recordIDs: recordIDs)
operation.fetchRecordsCompletionBlock = { (ckRecords, error) in
  ... // do stuff with your records
}
CKContainer.default().publicCloudDatabase.add(operation)