NSPredicate & relationship data?
NSPredicate & relationship data?
- Subject: NSPredicate & relationship data?
- From: Greg Robertson <email@hidden>
- Date: Mon, 4 May 2009 09:47:38 -0400
How do I create a search predicate to include an attribute of another
entity to which there is a relationship?
here is a simplified example of what I mean:
Entity Meal with the attributes: type and foods, foods has a one to
many relationship with the Entity Food
Entity Food with attributes: name and meal, meal has an inverse
relationship with the entity Meal
How can I create an NSPredicate to select all the distinct foods based
on the Meal attribute type?
I was thinking about something like:
-(void)initFindStuff:(NSInteger)TypeID
{
// standard setup
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Food"
inManagedObjectContext:userManagedObjectContext];
[request setEntity:entity];
// sort by name
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]
initWithObjects:sortByName, nil];
[request setSortDescriptors:sortDescriptors];
// this is where I am not sure
// *********************
NSPredicate *searchType = [NSPredicate
predicateWithFormat:@"type=%d", TypeID];
[request setPredicate:searchType];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:[NSArray arrayWithObjects:@"name",
@"meal.type", nil]];
// ***********************
// the rest is pretty standard stuff to setup an array
[sortDescriptors release];
[sortByName release];
NSError *error;
NSMutableArray *mutableFetchResults = [[userManagedObjectContext
executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil)
{
// Handle the error.
}
[self setMyArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
}
Can I use Obj-C 2.0 dot notation in an NSPredicate? If not how should
I setup the predicate?
Thanks
Greg
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden