Reputation: 129
I'm trying to use the NSPredicate
to only get specific objects. The problem is that all the objects are fetched and it should only be specific objects where the condition is correct.
- (void)fetchDevices {
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
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: @"selectedRow == %@" , selectedRowNumber];
devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[fetchRequest setPredicate:predicate];
}
The selectedRowNumber
is an int
declared in the .h
file.
A look on my Songs entity:
PS. Please comment below if more information is required and i'll provide it.
Upvotes: 2
Views: 620
Reputation: 4792
There is no need of setting Entity using setEntity
.
[fetchRequest setPredicate:predicate]
Just set predicate, it will work.
- (void)fetchDevices {
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Songs"];
// retrive the objects with a given value for a certain property
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"selectedRow == %@" , selectedRowNumber];
[fetchRequest setPredicate:predicate];
devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[fetchRequest setPredicate:predicate];
}
Upvotes: 0
Reputation: 1269
there seems to be two problems.
I think you need to put [fetchRequest setPredicate:predicate];
before devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
Also @"selectedRow == %d"
seems not right. You may want to change it to @"selectedRow = %d"
And no need to set entity again.
So the code may look like this.
- (void)fetchDevices {
int selectedRowNumber = 1;
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Songs"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"selectedRow = %d" , selectedRowNumber];
[fetchRequest setPredicate:predicate];
NSArray *devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
}
Let me know how it goes :)
Upvotes: 0
Reputation: 5182
Try this,
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"selectedRow = %@" , [NSNumber numberWithInt: selectedRowNumber]];
[fetchRequest setPredicate:predicate];
devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
Upvotes: 0
Reputation: 540085
You have to assign the predicate before executing the fetch request.
Also, if selectedRowNumber
is an int
then you have to use the %d
format
instead of %@
.
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"selectedRow == %d" , selectedRowNumber];
[fetchRequest setPredicate:predicate];
devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
Upvotes: 3