Thunk Thunk - 11 months ago 49
iOS Question

CKQuery using NSPredicate searching for NIL always throws an Invalid Expression error

On iOS 10.0.1 and Xcode 8.0, I have a

that is successfully returning data for a variety of conditions (
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"

When I set a breakpoint and examine
, 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"
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
, and
. All evaluate to
joiner == nil
and throw the exception.

In the first linked question, the OP uses
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?

Answer Source

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.