Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Problem - select group by/having max

Say I have two Entities:

Entities

Each Message belongs to a single MessageThread. How do I get all the message threads and the corresponding last message on that thread? Normally, in SQL, I'd do it as:

select __ from message group by thread having timeStamp=max(timeStamp)

For one, I don't think Core Data allows the @max in its predicates. Any ideas?

like image 883
jvliwanag Avatar asked Apr 26 '26 05:04

jvliwanag


1 Answers

This might be a bit old, but I had a similar problem recently. Here is my solution to the problem:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Message"];
request.predicate = [NSPredicate predicateWithFormat:@"timeStamp = [email protected]"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timeStamp" ascending:NO]];

I hope it helps...

like image 138
Gilad Novik Avatar answered Apr 27 '26 20:04

Gilad Novik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!