开发者

Core Data Problem - select group by/having max

开发者 https://www.devze.com 2023-02-10 09:16 出处:网络
Say I have two Entities: Each Message be开发者_运维知识库longs to a single MessageThread. How do I get all the message threads and the corresponding last message on that thread? Normally, in SQL, I

Say I have two Entities:

Core Data Problem - select group by/having max

Each Message be开发者_运维知识库longs 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?


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 = thread.messages.@max.timeStamp"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"timeStamp" ascending:NO]];

I hope it helps...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消