Nestor Velasco
Nestor Velasco

Reputation: 11

Comparing two key values with NSPredicate CloudKit

I am trying to write a predicate that compares two keys in a cloud kit database. So far I can compare one of the keys with a constant value. I want to use this to send a notification when the inventory of a product is low.

I have a record type called products which consists of a count that gets updated, and a minCount that never changes. I use this to display an "Inventory Low" label whenever count <= minCount.

Here is an example of the predicate I'm using with the constant value for the notifications:

let predicate = NSPredicate(format: "count <= %i", 10)

For one specific product I know that the minCount is 10, so this works properly for that one product, but the other products have different minCount values. Is there a way to do this?

Upvotes: 1

Views: 148

Answers (1)

Witek Bobrowski
Witek Bobrowski

Reputation: 4269

Since the CloudKit will expect the right hand side value to be a constant rather than a key, it will throw you the error you are mentioning in the comment to this answer if you to this:

let predicate = NSPredicate(format: "count <= minCount")

But according to this answer this should work

let predicate = NSPredicate(format: "%K <= %i", "count", "minCount")

Upvotes: 1

Related Questions