0

I have tried both these NSPredicates:

            let predicate = NSPredicate(format: “personID == %lld", id)
            let predicate = NSPredicate(format: “personID == %d", id)

I enabled SQL logging and saw this at the end of the SQL query:

WHERE  t0.ZPERSONID = ? 

In variables view, id has a value of Int64(156), but regardless of what I put in place of id, the resulting SQL query has t0.ZPERSONID = ? for its WHERE clause. Because of this, the predicate is useless and I’m getting duplicate record insertions every time. How do I get t0.ZPERSONID = 156?

9
  • you can check values also there in the log, but all values will be together comma separated you can put them in above query Commented May 11, 2017 at 9:32
  • @VarunNaharia I'm sorry, but I don't understand what you meant by that comment. What log are you referring to and where do I see these values? Commented May 11, 2017 at 9:34
  • @VarunNaharia The only thing SQL logging added to the console log in Xcode is that one query, of which I posted the WHERE clause. There's nothing else. Commented May 11, 2017 at 9:37
  • @Questioner: Set the Core Data debug level to 3 (see stackoverflow.com/a/12306537/1187415) and then you'll see what values the SQLite query is bound to. Commented May 11, 2017 at 9:39
  • 1
    That means that query is "WHERE t0.ZPERSONID = 2595". – If id is an Int64 then NSPredicate(format: "personID == %lld", id) should be correct. What does print(predicate) show? Commented May 11, 2017 at 9:55

1 Answer 1

1
WHERE  t0.ZPERSONID = ?

is (part of) the prepared statement. To see which values the parameters are bound to, set the Core Data debugging level to 3 (compare How to print Core Data debug values?):

-com.apple.CoreData.SQLDebug 3
-com.apple.CoreData.Logging.stderr 1

Also print(predicate) is useful to check the created predicate for correctness.

In your case the problem is that you pass an NSNumber instance as argument, but the %lld format expects an Int64.

You can convert the NSNumber to an Int64

let id = NSNumber(value: Int64(156))

let p1 = NSPredicate(format: "personID == %lld", id.int64Value)
print(p1) // personID == 156

or even simpler, use the %@ format instead:

let p2 = NSPredicate(format: "personID == %@", id)
print(p2) // personID == 156
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.