0

I'm performing query operation with Cloudkit, my record type is like this:

CKRecord Type: Person
{
  name STRING,
  age  INTEGER,
  friends  ARRAY,
}

Now, I want to query all person with no friends.I have tried

NSPredicate *predicate = [[NSPredicate alloc] initWithFormat: @"friends[SIZE] == 0"];
CKQuery *query = [[CKQuery alloc] initWithPredicate: predicate];

And

NSPredicate *predicate = [[NSPredicate alloc] initWithFormat: @"friends.@count == 0"];
CKQuery *query = [[CKQuery alloc] initWithPredicate: predicate];

The first one crashed with warning

friends[SIZE] is not an evaluated object expression

and second one returning error of

Invalid predicate

So , what is the correct way to query with array size?

Ray
  • 272
  • 2
  • 13
  • I don't see "count" or "size" in the list: https://developer.apple.com/documentation/cloudkit/ckquery?language=objc So I'm wondering if that's possible. – Larme Mar 19 '19 at 15:32
  • @Larme I found format 'array[SIZE]' in NSPredicate Programming Guide, but it won't work – Ray Mar 20 '19 at 01:58
  • From the doc: `The CKQuery class supports only a subset of the predicate behaviors offered by the full NSPredicate class.`, so that's maybe the case. – Larme Mar 20 '19 at 06:32
  • @Larme Thank you Larme, Now I have to add another field to track the array count, though ugly but works. – Ray Mar 20 '19 at 08:17
  • Apple highly recommends NOT using an array to store relationship data - it should be done with actual reference fields. Look at the 2016 WWDC video "CloudKit Best Practices" - starting around 33:30 they start talk a bit about why not to use an array to reference other records. Here's the link to the video: https://developer.apple.com/videos/play/wwdc2016/231 – Brian M Mar 27 '19 at 12:20

0 Answers0