Lance Samaria
Lance Samaria

Reputation: 19622

CKQuery -Specific Field with Specific Value

How can I query for this field myId with this specific id -MqXU-kKVhl5ef10XD

post:

let database = CKContainer(identifier: "iCloud.com.myCo.myApp").publicCloudDatabase

let record = CKRecord(recordType: "Sample")

record["myId"] = "-MqXU-kKVhl5ef10XD"

database.save(record) { (ckRecord, error) in
})

query:

let predicate = NSPredicate()
    
let query = CKQuery(recordType: "Sample", predicate: predicate)
    
database.perform(query, inZoneWith: nil) { (ckRecords, error) in
    if let err = error { return }

    guard let records = ckRecords else { return }

    guard let record = records.first else { return }

    guard let myId = record.value(forKey: "myId") as? String else { return }
    print(myId)
})

Upvotes: 0

Views: 73

Answers (1)

Lance Samaria
Lance Samaria

Reputation: 19622

found the answer here from @vanjakom. I had to make the myId field Queryable:

let predicate = NSPredicate(format: "myId == %@", "-MqXU-kKVhl5ef10XD")

Upvotes: 0

Related Questions