0
votes

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.

2
The strings are exact matches? Including capitalisation and white space? Are you really looking for an exact match or a partial match with case leniency? - Wain
It's an exact match and all the data is controlled by the app, there is no user input. - bitops
You'll need to show more info about the model, request and sample data. Presumably no error from the fetch? - Wain
Try to create a predicate only with name or series. May be it will help to localize a bug. - Avt
There are some ways t which can help you to debug your queries: the first is debugging/tracking the queries executed against the underlying sqlLite db using the Xcode flag [waynehartman.com/posts/…(-com.apple.CoreData.SQLDebug 1). The second is to see what data exactly are contained in the db using [github.com/ChristianKienle/Core-Data-Editor](Core Data editor) - LuckyStarr

2 Answers

1
votes

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.

-1
votes

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.