You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Core Data待办数据转换为SwiftUI图表可绘制的每日完成统计格式

Core Data 按日期分组统计每日完成待办数量实现方案

Core Data 本身支持原生的分组聚合统计能力,所有计算逻辑在数据库层完成,无需拉取全量数据手动遍历,性能远高于自行循环统计,实现步骤如下:

  • 构造按天截断的日期分组表达式
    首先需要将带时分秒的finishDate转换为仅保留年月日的日期值,作为分组依据:
// 日期截断表达式:清除时间部分,仅保留天维度
let dateTruncateExpr = NSExpression(forFunction: "cast:to:", arguments: [
    NSExpression(forKeyPath: "finishDate"),
    NSExpression(forConstantValue: "date")
])
let dateDesc = NSExpressionDescription()
dateDesc.name = "finishDay"
dateDesc.expression = dateTruncateExpr
dateDesc.expressionResultType = .dateAttributeType
  • 构造计数统计表达式
    统计每个日期分组下的待办数量:
// 分组计数表达式
let countExpr = NSExpression(forFunction: "count:", arguments: [NSExpression(forKeyPath: "objectID")])
let countDesc = NSExpressionDescription()
countDesc.name = "finishCount"
countDesc.expression = countExpr
countDesc.expressionResultType = .integer64AttributeType
  • 配置分组统计的Fetch请求
    指定请求返回字典类型的统计结果,过滤未完成的待办:
let fetchRequest: NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Todo")
// 过滤未完成的待办(finishDate 为空)
fetchRequest.predicate = NSPredicate(format: "finishDate != nil")
// 指定返回的两个字段:日期、对应完成数量
fetchRequest.propertiesToFetch = [dateDesc, countDesc]
// 按天维度分组
fetchRequest.propertiesToGroupBy = [dateDesc]
// 声明返回结果为字典类型,而非Todo实体对象
fetchRequest.resultType = .dictionaryResultType
  • 执行请求直接获取统计结果
    执行后直接得到按天分组的统计数据,仅需简单解析即可拿到图表需要的两个维度数据:
do {
    let result = try persistentContainer.viewContext.fetch(fetchRequest)
    var dateList: [Date] = []
    var countList: [Int] = []
    for item in result {
        guard let day = item["finishDay"] as? Date, 
              let count = item["finishCount"] as? Int else { continue }
        dateList.append(day)
        countList.append(count)
    }
    // dateList、countList 可直接传入图表组件使用
} catch {
    print("统计失败: \(error.localizedDescription)")
}

补充说明

如果需要限定统计的时间范围,直接在predicate中添加finishDate的起止条件即可;如果要在SwiftUI中自动响应数据变更,也可以直接把上述配置好的请求传入@FetchRequest包装器使用。

内容的提问来源于stack exchange,提问作者Mark Lakshman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 02:18:00