Reputation: 1598
Say I have two 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?
Upvotes: 3
Views: 1052
Reputation: 4604
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...
Upvotes: 3
Reputation: 3042
I could never get @max
to work and I still look for a better implementation.
What I do as a porkaround is to set the sort descriptor to order by date and then use the objectAtIndex:0
from the fetchedResults.
Upvotes: 0