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?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Questioner
  • 2,451
  • 4
  • 29
  • 50
  • you can check values also there in the log, but all values will be together comma separated you can put them in above query – Varun Naharia May 11 '17 at 09: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? – Questioner May 11 '17 at 09: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. – Questioner May 11 '17 at 09:37
  • @Questioner: Set the Core Data debug level to 3 (see http://stackoverflow.com/a/12306537/1187415) and then you'll see what values the SQLite query is bound to. – Martin R May 11 '17 at 09:39
  • @MartinR That added two more lines: `CoreData: details: SQLite bind[0] = 2595` and the query again with this information `returned 0 rows with values: ( )` – Questioner May 11 '17 at 09:47
  • Look at the saved `personID` and the string reprensentation resulting of `%lld", id` and compaire them. – shallowThought May 11 '17 at 09:55
  • 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? – Martin R May 11 '17 at 09:55
  • @MartinR `personID == -5764607523034232269` `id` is Int64(163) now – Questioner May 11 '17 at 10:06
  • @MartinR I seemed to have fixed the problem. While `id` had a value of `Int64(164)`, its type was `NSNumber` (this was chosen when the `NSManagedObject` subclass was generated). I cast it to `Int64` and now the SQL query has the correct value in it. Thanks for your help. Please post an answer so I can accept it. – Questioner May 11 '17 at 10:16

1 Answers1

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
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382