本文介绍了核心数据问题 - 选择组由/具有最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个实体:

每封邮件属于一个MessageThread。如何获取所有消息线程和相应的最后一条消息在该线程?通常,在SQL中,我会这样做:

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:

从消息组中选择__从具有timeStamp = max(timeStamp)的线程

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

对于一个,我不认为Core Data允许在其谓词中使用@max。任何想法?

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...

这篇关于核心数据问题 - 选择组由/具有最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:14