0

On iOS 10.0.1 and Xcode 8.0, I have a CKQuery that is successfully returning data for a variety of conditions (BEGINSWITH and equal to certain strings, etc.). Now I'm looking specifically for records with a null value in one of the fields.

According to Apple's Docs, plus How do I set up a NSPredicate to look for objects that have a nil attribute and NSPredicate with boolean is not comparing a boolean to NO, the following syntax should search for nil values:

NSPredicate *searchConditions = [NSPredicate predicateWithFormat:@"joiner = nil "];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"availableURLs"
                                           predicate:searchConditions];

When I set a breakpoint and examine searchConditions, it evaluates to joiner == nil. When I try to use it, the second line throws an exception:

Terminating app due to uncaught exception 'CKException', reason: 
'Invalid predicate: joiner == nil (Error Domain=CKErrorDomain Code=12
"Invalid right expression in <joiner == nil>: <nil> is not a function expression" 
UserInfo={ck_isComparisonError=true, 
NSLocalizedDescription=Invalid right expression in <joiner == nil>: <nil> is not a function expression, 
NSUnderlyingError=0x1706516d0 {Error Domain=CKErrorDomain Code=12 "<nil> is not a function expression" 
UserInfo={NSLocalizedDescription=<nil> is not a function expression, ck_isComparisonError=true}}})'

I've tried every combination of =, ==, nil, Nil, NIL, and NULL. All evaluate to joiner == nil and throw the exception.

In the first linked question, the OP uses predicateWithSubstitutionVariables to substitute in [NSNull null]. I tried that as well, but it evaluated again to joiner == nil and threw the same exception.

The Apple Docs have a section specifcally on searching for nil, and I can't see how their example differs from mine:

predicate = [NSPredicate predicateWithFormat:@"firstName = nil"];

What am I missing here?

Community
  • 1
  • 1
Thunk
  • 4,099
  • 7
  • 28
  • 47
  • 1
    I think the issue is more about `CKQuery` limitations that the `NSPredicate`, which would explain why you can use `nil` in `NSArray` of custom objects for instance... It's not clearly said, but from the Doc of `CKQuery`: `Building Your Predicates An NSPredicate object defines the logical conditions for determining whether a record is a match for a query. The CKQuery class supports only a subset of the predicate behaviors offered by the full NSPredicate class.` – Larme Oct 11 '16 at 07:46
  • @Larme Thanks, I just re-read the ckquery docs more closely. I previously missed the bit about only supporting a subset if nspredicate. If you'll post that as an answer, I'll accept and maybe help the next person that didn't read the docs close enough. – Thunk Oct 11 '16 at 16:08

1 Answers1

2

According to CKQuery doc, it seems that CKQuery doesn't allow nil in predicates (in iOS 10 at the time of writing the answer).

Building Your Predicates
An NSPredicate object defines the logical conditions for determining whether a record is a match for a query. The CKQuery class supports only a subset of the predicate behaviors offered by the full NSPredicate class.

Your NSPredicate construction didn't seem wrong, since it could work on NSArray of Custom Objects for instance.

Larme
  • 24,190
  • 6
  • 51
  • 81
  • 2
    So is there some other way to test for nil when building a predicate using CKQuery? – Z S Nov 16 '17 at 11:14