bitops
bitops

Reputation: 4292

FetchRequest with NSPredicate not finding results?

I am storing data in a table that has the columns "name" and "series". I am using this NSPredicate to query:

var request : NSFetchRequest = NSFetchRequest(entityName: "Entry")
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "name = %@ AND series = %@", name, series)
return request

The 'name' and 'series' variables are passed in as String arguments, which are also the data types on the table.

For some reason, this query returns no data. If I do a fetch request on this table without specifying a predicate I can see that the data is indeed there. I am not sure what I'm doing wrong.

For what it's worth I have tried enclosing the conditionals in parens but that didn't seem to make a difference either. Thanks in advance!

UPDATE: I've tried many different things but so far nothing is working. I'm really stumped.

For what it's worth it seems like I am having the same issue as this person: NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

But, there isn't anything on that entry stating specifically what the solution to the problem was.

Upvotes: 0

Views: 389

Answers (2)

bitops
bitops

Reputation: 4292

Oh wow...thank you everyone so much for all your answers. In the end it turned out that when I was populating the id for inserting a new row into the table, I was looking at a different table to calculate the new primary key id.

So I was overwriting existing records, which is why my query kept failing.

Kids, if you copy/paste your code without rigorously checking it, you're gonna have a bad time.

Upvotes: -1

Mundi
Mundi

Reputation: 80271

You can check if the data is there by

  1. printing the URL of the application documents directory to the console.
  2. going to this directory in Terminal
  3. running sqlite3 <databaseName>
  4. trying select * from z<entityName> where name = '<nameData>'

You will be able to explore the data and check if it contains what you expect. Don't forget to also check the content of your name and series variables in the code.

Upvotes: 1

Related Questions