我正在撰写费用跟踪器应用,这是我的数据模型:
在我显示费用周期性(例如每日)摘要的一个屏幕中,我想使用NSFetchedResultsController
来获取所有类别以及每个期间的总费用 >(例如每天)。句点由两个NSDate
变量定义 - startDate
和endDate
,因为我也会有每周和每月的观看次数 - 我认为应该依赖于NSFetchedResultsController
中的摘要。即使总和为零,我也希望NSFetchedResultsController
返回类别。我不知道如何设置它。
我尝试过:我已在NSFetchedResultsController
实体上成功构建Category
并使用NSExpressionDescription
获取其expenses
的总数关系,但总计所有费用,我无法找到指定包容期的方法。
let request = NSFetchRequest(entityName: Category.entityName)
request.resultType = .DictionaryResultType
request.propertiesToFetch = [
"name", "color",
{
let totalColumn = NSExpressionDescription()
totalColumn.name = "total"
totalColumn.expression = NSExpression(format: "@sum.expenses.amount")
totalColumn.expressionResultType = .DecimalAttributeType
return totalColumn
}()
]
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
let fetcher = NSFetchedResultsController(fetchRequest: request, managedObjectContext: App.state.mainQueueContext, sectionNameKeyPath: nil, cacheName: nil)
我也一直在阅读有关已获取属性的内容,但我也无法找到在startDate
之前在谓词中指明endDate
和NSFetchedResultsController
的方法解雇了。帮助
答案 0 :(得分:0)
您可以为获取请求添加谓词:
request.predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];