Swift CoreData聚合一对多关系
我有一个具有以下“体系结构”的CoreData模型:我有一个Swift CoreData聚合一对多关系,swift,core-data,one-to-many,Swift,Core Data,One To Many,我有一个具有以下“体系结构”的CoreData模型:我有一个会话实体,属性为系统版本。然后我有一个Views(Session>Views)实体,带有delta\u-time属性。最后,我有一个“UI\u元素”(视图>UI\u元素)实体,带有变量值属性。我想根据特定的变量值值过滤内容,然后显示增量时间按系统版本分组的平均值 我尝试了以下实现: var expressionDescriptions = [AnyObject]() expressio
会话
实体,属性为系统版本
。然后我有一个Views(Session>Views)
实体,带有delta\u-time
属性。最后,我有一个“UI\u元素”(视图>UI\u元素)
实体,带有变量值
属性。我想根据特定的变量值
值过滤内容,然后显示增量时间
按系统版本
分组的平均值
我尝试了以下实现:
var expressionDescriptions = [AnyObject]()
expressionDescriptions.append("views_ui.sessions.systemVersion" as AnyObject)
let expressionDescription = NSExpressionDescription()
expressionDescription.name = "Average Time"
expressionDescription.expression = NSExpression(format: "@avg.views_ui.delta_time_number")
expressionDescription.expressionResultType = .integer32AttributeType
expressionDescriptions.append(expressionDescription)
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "UI_Elements")
fetch.predicate = requete
fetch.propertiesToGroupBy = ["views_ui.sessions.systemVersion"]
fetch.resultType = .dictionaryResultType
fetch.sortDescriptors = [NSSortDescriptor(key: "views_ui.sessions.systemVersion", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))]
fetch.propertiesToFetch = expressionDescriptions
var results:[[String:AnyObject]]?
var expressionDescriptions=[AnyObject]()
expressionDescriptions.append(“views\u ui.sessions.systemVersion”作为AnyObject)
let expressionDescription=NSExpressionDescription()
expressionDescription.name=“平均时间”
expressionDescription.expression=NSExpression(格式:“@avg.views\u ui.delta\u time\u number”)
expressionDescription.expressionResultType=.integer32AttributeType
expressionDescription.append(expressionDescription)
let fetch=NSFetchRequest(entityName:“UI_元素”)
fetch.predicate=requete
fetch.propertiesToGroupBy=[“视图\u ui.sessions.systemVersion”]
fetch.resultType=.dictionaryResultType
fetch.sortDescriptors=[NSSortDescriptor(键:“视图”\u ui.sessions.systemVersion),升序:true,选择器:#选择器(NSString.localizedStandardCompare(:))]
fetch.propertiesToFetch=expressionDescriptions
变量结果:[[String:AnyObject]]?
不幸的是,我得到了以下错误:
CoreData:错误:SQLCore dispatchRequest:异常处理请求:
,无效的密钥路径(请求
对于仅toOne键路径上的聚合操作):
用户信息为(空)的视图\u ui.delta\u时间\u编号
我最终通过修改代码获得了预期的结果,如下所示:
let keypathExp = NSExpression(forKeyPath: "views_ui.delta_time_number")
expressionDescription.expression = NSExpression(forFunction: "average:", arguments: [keypathExp])