Re: Using aggregate functions in a Core Data Fetch Request
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