0

I have a Core Data importer that loops through data, and ignores duplicate records during the import process.

But I have discovered that my NSFetchRequest is not matching against recently stored records that have not been saved. And I am seeing seemingly identical queries delivering different and unexpected results.

For example, in my testing, I discovered that this query matches and returns results:

fetchTest.predicate = [NSPredicate predicateWithFormat:@"%K = 3882", @"intEmployee_id"];

But this seemingly identical one does not:

fetchTest.predicate = [NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id", @"3882"];

But - they both match identically after the context is saved to the persistent store.

Apple's documentation says that fetches should work against pending changes by default, and indeed I have conformed that [fetchTest includesPendingChanges] = YES.

Any ideas what on earth is going on here? How is it possible that those two fetches return different results?

radven
  • 2,296
  • 1
  • 22
  • 39

2 Answers2

1

Maybe the employee id is not a string but a number? Then the predicate should be:

[NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id",
                                             [NSNumber numberWithInt:3882]];

This would imply that the erratic behavior comes from mixing up the types. It still works somehow, even if erratically, because in the SQLite docs it says that SQLite actually does not really distinguish by type when storing the data physically.

See Distinctive Features of SQLite from the SQLite web site, under the heading Manifest Typing.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I think you might be on to something... I am used to SQLite not distinguishing by type, and all my Core Data experiments up to now have had no issues assemble a predicate out of strings even if the data field underneath is a number. – radven May 06 '12 at 22:00
  • Then this must be it. Core Data intentionally hides the database layer. The behavior anticipated by you is not predictable. The proper way to query the ID is the code above. Try it and let me know if it fixes the problem. – Mundi May 07 '12 at 10:38
1

These actually don't evaluate to the same value.

[NSPredicate predicateWithFormat:@"%K = 3882", @"intEmployee_id"]

evaluates to intEmployee_id = 3882 while

[NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id", @"3882"]

evaluates to intEmployee_id = "3882"

Try using a number instead of a string for the id.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Indeed - I noticed that they evaluate to those two different query strings, but they perform identically after the database has been saved to the persistent store, but not before. – radven May 06 '12 at 21:58
  • 1
    I would file this as a bug with Apple, as they should have the same behavior before and after saving... Even though they **are** different, they probably should evaluate the same. – lnafziger May 06 '12 at 22:22
  • Great! Please update when you get a response. Does using a NSNumber work in the meantime? – lnafziger May 08 '12 at 04:04