【发布时间】:2015-11-13 18:26:59
【问题描述】:
let fetchRequest = NSFetchRequest(entityName: "Product")
fetchRequest.predicate = NSPredicate(format: "shopItem.wishList == %@", currentWishList)
fetchRequest.resultType = .DictionaryResultType
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "mainBestPrice", ascending: true)]
let minPriceExpression = NSExpression(format: "min:(mainBestPrice)")
let minPriceED = NSExpressionDescription()
minPriceED.expression = minPriceExpression
minPriceED.name = "productPrice"
minPriceED.expressionResultType = .DoubleAttributeType
fetchRequest.propertiesToFetch = ["shopItem.keyword", "productTitle", minPriceED]
fetchRequest.propertiesToGroupBy = ["shopItem.keyword"]
我想用NSFetchRequest按一个字段分组,但总是报错:
CoreData:错误:严重的应用程序错误。异常被捕获 在核心数据更改处理期间。这通常是一个错误 NSManagedObjectContextObjectsDidChangeNotification 的观察者。 带有 GROUP BY 组件的查询中的 SELECT 子句只能包含 在 GROUP BY 或聚合函数中命名的属性
我应该在分组中使用来自propertiesToFetch 的所有字段。但我不想这样做......
【问题讨论】:
-
没有办法解决这个问题。您必须将
productTitle添加到propertiesToGroupBy,或者将其从propertiesToFetch中删除。可能你可以通过不同的方式获得你想要的东西:要么使用键值编码集合运算符,要么使用 NSFetchedResultsController。如果您编辑帖子以包含实体和关系的详细信息(一对多、多对多),我相信您会得到一些建议。 -
我真的需要所有字段,你能告诉我如何在
fetchRequest之后对结果进行分组吗?