问题
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: "" }
}]
回答1:
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.
来源:https://stackoverflow.com/questions/32683691/how-do-i-query-for-nil-values-with-cloudkit-js