NSPredicate for NSNumber column

Probably something simple I'm missing, but I'm trying to write a very simple predicate - return all rows from a table where an NSNumber column value = 3.


I've tried many variations of the following with no success:


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number = %@", @"3"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number == %@", @"3"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number = %@", @3];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number == %@", @3];


Can someone help me to recognize the rror of my ways, please?


Thanks in advance!

What's the rest of your code to build the fetch request and execute it look like? Because

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number == %@", @3];

should work if your attribute name is correct_number and has been declared properly.

Here's the whole method :


    self.myInfoMO = [NSEntityDescription
                     insertNewObjectForEntityForName:@"MyInfo"
                     inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyInfo" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number == %@", @3];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    // NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
    // [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];

    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects != nil && fetchedObjects.count > 0)
    {
        // Fill Fields with Data
        // Is data returned in myInfoMO, or in fetchedObjects?
        // And if in fetchedObjects, what structure/fields/objects?
    }


I don't need the sort descriptor in this particular query as I'm certain to only bring back one row.


Also, while I'm being foolish, since this is my first time fetching data through CoreData, does the data get returned in the fetchedObjects array (and if so, in what object components) or in the myInfoMO.column_name(s), just as I place it in the myInfoMO to save it in the db?


Thanks again for your help - I really appreciate it!

By way of additional info that I realized I should show you, here's the main issue:


    self.myInfoMO = [NSEntityDescription
                     insertNewObjectForEntityForName:@"MyInfo"
                     inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyInfo" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"correct_number == %@", @3];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    // NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
    // [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; 

    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects != nil && fetchedObjects.count > 0)
    {
        // Fill Fields with Data
        // Is data returned in myInfoMO, or in fetchedObjects?
        // And if in fetchedObjects, what structure/fields/objects?
        self.myButton.correctValue = self.myInfoMO.correct_number;
    }


When I've corrected the NSPredicate to the version you've shown above, I do get a response of fetchedObjects.count = 1. So I'm getting data returned. And I know the correct_number value is 3, from what I entered and then verified by looking at the SQLite database with SQLite Datum (I use this to verify the data actually saved from my entry function which is working OK).


The myInfo.correct_number is defined in the data model as an Integer64.

the myButton.correctValue property is defined as an NSNumber.


After checking the fetch to the myButton.correctValue property/variable, I see it has a value of (long) ). Is it possible that this is an incorrect use of Integer64 to NSNumber conversion? Do I need to change the property type to a long or an NSInteger? (I'll try this in the meantime to see what I can figure out.)


I guess I'm now unsure of whether the issue is my CoreData code or my property/column definitions.


Thanks NotMyName!

Accepted Answer

To answer the question in the comments of your code:

  1. // Fill Fields with Data
  2. // Is data returned in myInfoMO, or in fetchedObjects?
  3. // And if in fetchedObjects, what structure/fields/objects?
  4. self.myButton.correctValue = self.myInfoMO.correct_number;

The data is returned in the fetchedObjects array.


Why are you creating a entity and assigning it to self.myInfoMO in the first three lines of that code? On line 21, when you do self.MyButton.correctValue = self.myInfoMO.correct_number, what you're getting is the default value that's in the object you created at the start, not the value of any of the objects in the fetch result.


So you probably should be using something like

  1. if (fetchedObjects != nil && fetchedObjects.count > 0)
  2. {
  3. MyInfo *result = (MyInfo *) ([fetchedObjects objectAtIndex:0]);
  4. // or assign self.myInfoMO = [fetchObjects objectAtIndex: 0]; and add the cast if the compiler complains
  5. self.myButton.correctValue = result.correctNumber;
  6. }

Awesome! Thanks so much NotMyName! Now I see what's happening and how it works. It's funny how an example in real life makes all the difference. I'm not sure why I didn't get this from the documentation, but I do understand why the Recipe and Earthquake (Threaded CoreData) examples haven't been what I need. For what I'm doing, each button has 3 values to be loaded from the database so there's no 1:1 column to field equivalence, plus I'm essentially doing a "detail view" and not able to use a TableViewController.


I really appreciate your time in showing me what I needed!

NSPredicate for NSNumber column
 
 
Q