0

I have to filter out some data from a list in a Realm database. I have saved record of students in database. I have seen it and confirm it using Realm database browser. To filter data from database Realm used predicate in their examples under the DOCS and samples. So the following is the predicate where my app is crashing:

let resultPredicate = NSPredicate(format: "IsRecordDeleted == false AND StdListId == %@ AND UserId == %@ AND Status == %@", stdModel.Id, 45,Status.Active.rawValue)

here Status.Active.rawValue is my enum that is returning an integer value.

If I remove the following part from the string predicate it runs as expected:

let resultPredicate = NSPredicate(format: "IsRecordDeleted == false AND StdListId == %@",stdModel.Id)

So I'm thinking that I might have some value null or anything wrong with the values etc. I have double checked it, but all values are there and were right as expected.

I am really clueless as there is no stacktrace or log whatever you can say it. I am new to iOS and Swift. I tried to read Predicate on Apple doc but those were in Objective-C.

Is there someone who can simply tell me what I am doing wrong and what should be done when writing the predicate? Or at least give me one example to write predicate having multiple placeholders and values.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Android teem
  • 780
  • 2
  • 13
  • 36
  • 1
    If `Status.Active.rawValue` is an `Int`, then the placeholder for it should be `%d`, not `%@` and not causes a EXEC_BAD_ACCESS error. – Larme Oct 02 '18 at 09:23
  • @Larme its not that case – Android teem Oct 02 '18 at 09:31
  • `let resultPredicate = NSPredicate(format: "Status == %d", Status.Active.rawValue)`, crashes then? What is `Status.Active` really? And what's the type of `Status.Active.rawValue`? – Larme Oct 02 '18 at 09:33
  • What happens if you hard code the `Status.Active.rawValue` in the Predicate? (like: `let resultPredicate = NSPredicate(format: "IsRecordDeleted == false AND StdListId == %@ AND UserId == %@ AND Status == 1", stdModel.Id, 45)` – Lefteris Oct 02 '18 at 10:00
  • 2
    Note that `AND UserId == %@` with `45` should also create a crash. It should be `%d` too. So `let resultPredicate = NSPredicate(format: "IsRecordDeleted == false AND StdListId == %@ AND UserId == %@ AND Status == %@", stdModel.Id, 45,Status.Active.rawValue)` should be `let resultPredicate = NSPredicate(format: "IsRecordDeleted == false AND StdListId == %@ AND UserId == %d AND Status == %d", stdModel.Id, 45,Status.Active.rawValue) ` – Larme Oct 02 '18 at 10:18
  • thanks it solved. You can answer it so that I can accept that – Android teem Oct 02 '18 at 10:35
  • I won't answer it because it's a duplicate of the linked question. It's the same reason, I just failed previously to see that there were twice the issue because you focused me on the last part, the the 3rd part also create issue. – Larme Oct 02 '18 at 15:56

0 Answers0