[英]How can I sort Core Data records before GROUP BY?
Imagine I have this Core Data entity想象一下我有这个核心数据实体
@objc(SomeEntity)
class SomeEntity: NSManagedObject {
@NSManaged var date: Date
@NSManaged var timeZoneIdentifier: String
@NSManaged var name: String
@NSManaged var brand: String
}
I want to fetch all rows with distinct name
and brand
fields, but for each one, I want the latest date
and the timeZoneIdentifier
for that latest date
row.我想获取具有不同
name
和brand
字段的所有行,但对于每一行,我想要最新date
行的最新date
和timeZoneIdentifier
。
Here's some example data:以下是一些示例数据:
date![]() |
timeZoneIdentifier![]() |
name![]() |
brand![]() |
---|---|---|---|
2020-01-01 ![]() |
America/Los_Angeles![]() |
Name 1![]() |
Brand 1![]() |
2020-01-02 ![]() |
America/Tiajuana![]() |
Name 1![]() |
Brand 1![]() |
2020-01-03 ![]() |
America/Denver![]() |
Name 1![]() |
Brand 2![]() |
2020-01-04 ![]() |
America/Phoenix![]() |
Name 2![]() |
Brand 1![]() |
I essentially want to fetch .dictionaryResultType
results that will look like this:我本质上想获取如下所示的
.dictionaryResultType
结果:
lastDate![]() |
lastDateTimeZoneIdentifier ![]() |
name![]() |
brand![]() |
---|---|---|---|
2020-01-02 ![]() |
America/Tiajuana![]() |
Name 1![]() |
Brand 1![]() |
2020-01-03 ![]() |
America/Denver![]() |
Name 1![]() |
Brand 2![]() |
2020-01-04 ![]() |
America/Phoenix![]() |
Name 2![]() |
Brand 1![]() |
Which you could do with this SQL query:您可以使用此 SQL 查询执行以下操作:
SELECT * FROM (SELECT * FROM SomeEntity ORDER BY date DESC) as sub GROUP BY sub.name, sub.brand;
Can I achieve the same thing with Core Data with minimal queries?我可以通过最少的查询使用 Core Data 实现相同的目标吗? I can't find any examples or documentation online trying to do something like this.
我在网上找不到任何示例或文档来尝试做这样的事情。
I can write a NSFetchRequest
that essentially executes this:我可以编写一个基本上执行此操作的
NSFetchRequest
:
SELECT date, timeZoneIdentifier, name, brand FROM SomeEntity GROUP BY name, brand ORDER BY date;
But that orders after the GROUP BY, not before.但是那个命令在 GROUP BY 之后,而不是之前。
I can also write a NSFetchRequest
that essentially executes this:我还可以编写一个基本上执行此操作的
NSFetchRequest
:
SELECT max(date), name, brand FROM SomeEntity GROUP BY name, brand;
Which almost works only because I wanted the latest date, but of course I lose the timeZoneIdentifier
that the row came from.这几乎只是因为我想要最新的日期,但我当然会丢失该行来自的
timeZoneIdentifier
。 I have written up this example as a gist that you can throw into a Swift playground. 我把这个例子写成一个要点,你可以把它扔到 Swift 操场上。
Thanks!谢谢!
As you've probably discovered, you cannot specify any propertiesToFetch
other than the properties you group by, and aggregate functions.您可能已经发现,除了分组依据的属性和聚合函数之外,您不能指定任何
propertiesToFetch
。 Hence you can't include the timeZoneIdentifier
.因此,您不能包含
timeZoneIdentifier
。
However, you can include an NSExpression
that represents the evaluated object (see NSExpression.expressionForEvaluatedObject
).但是,您可以包含一个
NSExpression
来表示已评估的 object(请参阅NSExpression.expressionForEvaluatedObject
)。 This will give you the objectID for the object with the max(date)
- which you can then fetch to get the other property values.这将为您提供带有
max(date)
的 object 的 objectID - 然后您可以获取它以获取其他属性值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.