user3195388
user3195388

Reputation: 129

Predicate fetch all objects

I'm trying to make a NSPredicate for getting specific objects. The problem is that the NSPredicate fetch all objects instead of only the specific objects. What am i doing wrong?

my code look like this:

- (void)fetchDevices {
    // Fetch the devices from persistent data store

    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Songs"];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Songs" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    // retrive the objects with a given value for a certain property
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%d == %d", currentRow,      [device valueForKey:@"selectedRow"]];

    [fetchRequest setPredicate:predicate];

}

Properties and variables:

@property (strong) NSMutableArray *devices;
currentRow = indexPath.row;
[device valueForKey:@"selectedRow"] 

[device valueForKey:@"selectedRow"] (This is a INT which is stored in my core data entity)

Upvotes: 1

Views: 342

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70976

It's hard to tell from your question, because it's not clear what attributes exist on the Songs entity or how you want to match them. However the main problem with this predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"%d == %d", currentRow,      [device valueForKey:@"selectedRow"]];

...is that it does not attempt to match anything on the Songs entity. The predicate string needs to include one of the attribute names from the entity type you're fetching. Or, if you have the attribute name in a local variable (maybe that's what [device valueForKey:@"selectedRow"] is?) you need to use %K in the predicate. What you have is a predicate where both sides are integer values, and I guarantee that the attribute name is not an integer.

Again, I can't tell you what your predicate should be without a better explanation of what you're trying to do. But the left side of the format string does need either a literal attribute name or a %K matching an attribute name in order to filter effectively.

Upvotes: 1

Related Questions