iosswiftdatabasecloudkitckrecord

Fetching Multiple Records by Record Name


I have an array of record names (or strings) . I want to fetch any record in the public database which has any of these record names. How might I do this?


Solution

  • You should apply something like this...

    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 ;)
    })
    

    The NSPredicate uses the aggregated operator IN in order to filter by the contents of an array. More info at Predicate Programming Guide