简体   繁体   English

如何在 GROUP BY 之前对 Core Data 记录进行排序?

[英]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.我想获取具有不同namebrand字段的所有行,但对于每一行,我想要最新date行的最新datetimeZoneIdentifier

Here's some example data:以下是一些示例数据:

date日期 timeZoneIdentifier时区标识符 name姓名 brand
2020-01-01 2020-01-01 America/Los_Angeles美国/洛杉矶 Name 1名称 1 Brand 1品牌1
2020-01-02 2020-01-02 America/Tiajuana美国/蒂华纳 Name 1名称 1 Brand 1品牌1
2020-01-03 2020-01-03 America/Denver美国/丹佛 Name 1名称 1 Brand 2品牌 2
2020-01-04 2020-01-04 America/Phoenix美国/凤凰 Name 2名称 2 Brand 1品牌1

I essentially want to fetch .dictionaryResultType results that will look like this:我本质上想获取如下所示的.dictionaryResultType结果:

lastDate最后日期 lastDateTimeZoneIdentifier lastDateTimeZoneIdentifier name姓名 brand
2020-01-02 2020-01-02 America/Tiajuana美国/蒂华纳 Name 1名称 1 Brand 1品牌1
2020-01-03 2020-01-03 America/Denver美国/丹佛 Name 1名称 1 Brand 2品牌 2
2020-01-04 2020-01-04 America/Phoenix美国/凤凰 Name 2名称 2 Brand 1品牌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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM