• 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: Using aggregate functions in a Core Data Fetch Request
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using aggregate functions in a Core Data Fetch Request


  • Subject: Re: Using aggregate functions in a Core Data Fetch Request
  • From: Jamie Hardt <email@hidden>
  • Date: Fri, 26 Sep 2008 22:05:25 -0700

What about taking your objects and sorting them by value, and popping the last object?

NSFetchPredicate *fr = /* lets say this is setEntity to your entity */;
NSManagedObjectContext *moc = /* your moc here */;

[fr setSortDescriptor: [[[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES] autorelease]];

NSArray *result = [moc executeFetchRequest:fr error:nil];

id maxObj = [result lastObject];

If you're using the "id" as a sort of UID, however, I would just use CFUUIDCreate() to make an ID that's guaranteed to be unique without quizzing the data store.


On Sep 26, 2008, at 12:25 PM, Fred McCann wrote:

I'm attempting to find an object that has a maximum value in the data store. In sql, the query would be something like this:

select * from foo where value = select max(value) from foo.

Actually, I would be thrilled if I could just get select max(value) from foo.

I've read through the Core Data and Predicates pdfs, and there is a mention of a "max" function in the "BNF Definition of Cocoa Predicates" of the Predicates document, but I've had no luck. My current attempt is to create a fetch request in the XCode Data Modeler using the predicate:

id == max:(
   id
)

When I try to execute this, I get this error:

Unable to generate SQL for predicate (id == max:(
   $FETCHED_PROPERTY.id
)) (problem on RHS)

I'm currently using Sqlite as the backing store.

Does anyone have any suggestions on how to do this? Is this even possible? What I'm trying to accomplish is to maintain ids across multiple persistent stores that can weakly linked as fetched properties.


- Fred McCann email@hidden


_______________________________________________

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:
@gmail.com


This email sent to email@hidden

_______________________________________________

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


References: 
 >Using aggregate functions in a Core Data Fetch Request (From: Fred McCann <email@hidden>)

  • Prev by Date: Re: [NSCFNumber intValue]: unrecognized selector
  • Next by Date: Re: problems in runModal in NSOpenPanel used with defaults
  • Previous by thread: Using aggregate functions in a Core Data Fetch Request
  • Next by thread: NSURLConnection issues
  • Index(es):
    • Date
    • Thread