0

I have a record type called User, and that user may or may not have an address.

I'm trying to set a predicate, so I get all Records with no address..

I've tried the following with no luck:

let predicate = NSPredicate(format: "address != %@", "")
let predicate = NSPredicate(format: "address.length > 0")

None of these work.. I'm sure there has to be a way? In my cloudkit dashboard, address is a String, with no value for some.

JDM
  • 883
  • 1
  • 8
  • 20
  • Have you tried `let predicate = NSPredicate(format: "address = nil")`? – Pierce Jan 15 '17 at 00:50
  • NVM I don't think that works with CloudKit – Pierce Jan 15 '17 at 00:54
  • Nope, but just tried anyway and it doesn't work, – JDM Jan 15 '17 at 00:55
  • I found this for you. I'm sure it's not what you want to hear http://stackoverflow.com/a/32731924/5378116 – Pierce Jan 15 '17 at 01:03
  • Honestly if it seems like it's impossible to query for `nil` value with CloudKit, your best bet would just be to run a query for ALL the "User" records, and then in the completion block only handle those without a value for `address` – Pierce Jan 15 '17 at 01:05
  • Thanks @Pierce - I thought about this, and for now it could work, but I worry about performance when a lot of users exist... Thanks! – JDM Jan 15 '17 at 01:06
  • Yeah I know. It's note exactly efficient. I know I've wanted to pull my hair out before using CloudKit for queries with predicates. It has it's upsides being so user friendly, but some serious down-sides as well. – Pierce Jan 15 '17 at 01:08
  • Yep.. I did this: let newRecords = records?.filter { !(($0.object(forKey: "address") as? String) ?? "").isEmpty } Because I noticed sometimes the key is not there, and sometimes it's an empty string if I modified it myself.. so this handles nils and empty string – JDM Jan 15 '17 at 01:11
  • It's not a CloudKit solution, but if you feel like learning a seriously powerful cloud platform in the future, check out Amazon Web Services. Just beware the learning curve is intense. I started pursuing my AWS credential recently, and now I don't think I could go back to CloudKit – Pierce Jan 15 '17 at 01:17

0 Answers0