1

Using CloudKit.js, how do I construct a query that matches items where a field is nil? Every permutation I've tried fails - either it's clearly matching on a string value (i.e. "null" or "nil") or it throws an error if I actually try to pass 'null'.

Any ideas? None of the below work.

filterBy: [{
        fieldName: 'customerNumber',
        comparator: 'EQUALS',
        fieldValue: { value: "nil" }
}]

filterBy: [{
        fieldName: 'customerNumber',
        comparator: 'EQUALS',
        fieldValue: { value: null }
}]

filterBy: [{
        fieldName: 'customerNumber',
        comparator: 'EQUALS',
        fieldValue: { value: "null" }
}]

filterBy: [{
        fieldName: 'customerNumber',
        comparator: 'EQUALS',
        fieldValue: { value: "" }
}]
Hunter
  • 4,273
  • 5
  • 37
  • 44

1 Answers1

6

I don't think there is any option to query for nil values in CloudKit. Also not from native code. I have tried a lot of predicates, but non seems to work.

You do have to be aware that CloudKit is a key-value store. When a value is nil, then the key won't be there in the record. There is no option to query for non existing keys.

The closest I have come to a good solution was with a predicate with the format "NOT myField => ''" sorry, don't know the format for .js

In the CloudKit CKQuery documentation there is nothing mentioned about nil fields.

In the documentation for NSPredicates there are sample's for nil values.

But it was already clear that CloudKit has implemented only a subset of the possibilities of NSPredicate (see CKQuery class reference)

I think the best way to handle this is by not using/depending on nil values. If you do need to check some nil status, then instead you could add an extra field that would represent that status.

Edwin Vermeer
  • 12,666
  • 2
  • 31
  • 56
  • You may be right. I submitted this to DTS and will either accept your answer or submit whatever their alternative ends up being. Thanks. – Hunter Sep 23 '15 at 19:57
  • 1
    Heard back from DTS and you are indeed correct. I guess it's time to file an enhancement Radar. – Hunter Sep 28 '15 at 17:57