Re: Doing a NSFetchRequest on a single NSManagedObject
Re: Doing a NSFetchRequest on a single NSManagedObject
- Subject: Re: Doing a NSFetchRequest on a single NSManagedObject
- From: Charilaos Skiadas <email@hidden>
- Date: Sat, 6 Aug 2005 11:23:42 -0500
On Aug 6, 2005, at 10:48 AM, Andrew Sage wrote:
My data model consists of an Entity called Project which has a one
to many relationship to
an Entity called Task. Task has an attribute called completedDate.
I am wanting to get the number of Tasks that do not have a
completedDate set.
Project is represented by a class called Project.
I have created the following method in Project:
-(int)tasksCount
{
int total = 0;
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Task"
inManagedObjectContext:moc];
NSPredicate * predicate;
predicate = [NSPredicate predicateWithFormat:@"completedDate =
NIL"];
NSFetchRequest * fetch = [[NSFetchRequest alloc] init];
[fetch setEntity: entity];
[fetch setPredicate: predicate];
NSArray * results = [moc executeFetchRequest:fetch error:nil];
[fetch release];
total = [results count];
return total;
}
However, this returns the number of Tasks that do not have a
completedDate set for all the Projects
and not just the current one.
Well, yes, since you never told it to restrict to the ones that
correspond to your preferred project. I am assuming your to-many
relationship has a to-one inverse, maybe called "project", that
returns the corresponding project the task is in. If not, create one.
Then your predicate should probably be:
[NSPredicate predicateWithFormat:@"(completedDate = NIL) AND (project
= %@)",self];
Thanks.
Andrew
Haris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden