• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Doing a NSFetchRequest on a single NSManagedObject
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Follow-Ups:
    • Re: Doing a NSFetchRequest on a single NSManagedObject
      • From: Andrew Sage <email@hidden>
    • Re: Doing a NSFetchRequest on a single NSManagedObject
      • From: Andrew Sage <email@hidden>
References: 
 >Doing a NSFetchRequest on a single NSManagedObject (From: Andrew Sage <email@hidden>)

  • Prev by Date: Re: Reverse engineering
  • Next by Date: Re: Strange "exit due to Signal 11"
  • Previous by thread: Doing a NSFetchRequest on a single NSManagedObject
  • Next by thread: Re: Doing a NSFetchRequest on a single NSManagedObject
  • Index(es):
    • Date
    • Thread