简体   繁体   English

如何在不同时间段内跟踪数据(快速)-计数器/习惯跟踪应用程序

[英]How to keep track of data over various periods of time (Swift) - Counter/Habit Tracking App

I'm trying to create a simple counter/habit tracking app that allows users to see how many times they've completed a task over a certain amount of time (daily/weekly/monthly/yearly/total). 我正在尝试创建一个简单的计数器/习惯跟踪应用程序,使用户可以查看他们在一定时间内(每天/每周/每月/每月/每年/总计)完成任务的次数。

My question is: what is the best way to track variables over time such that when the user selects a time period, the variable's value can update to reflect that change? 我的问题是:随时间推移跟踪变量的最佳方法是什么,以便当用户选择一个时间段时,变量的值可以更新以反映该更改?

A few examples to clarify what I mean: 一些例子可以澄清我的意思:

  • A user who initially created a monthly timer to track workouts should be able to switch to a weekly timer and see the data update as needed (there should be fewer in a week than in a month). 最初创建用于跟踪锻炼的每月计时器的用户应该能够切换到每周计时器,并根据需要查看数据更新(一周中应少于一个月)。
  • A user who has created a daily counter should see the daily counter reset to 0 at midnight each day, with the previous day's value visible to compare. 创建了每日计数器的用户应该看到每日计数器在每天的午夜重置为0,并且可以比较前一天的值。
  • A user who has created a "total" counter should be able to switch to any time period (daily/weekly/monthly/yearly) and see the counter update as needed. 创建“总计”计数器的用户应能够切换到任何时间段(每天/每周/每月/每年),并根据需要查看计数器更新。

The UI for my app is basically a custom tableview with the counter number, counter name, and the time period over which the counter number is being tracked. 我的应用程序的UI基本上是一个自定义表格视图,其中包含计数器编号,计数器名称以及跟踪计数器编号的时间段。 A detail view allows users to add to/subtract from the counter's value. 详细信息视图允许用户添加/减去计数器的值。

I have been wracking my brain and Google for a long time, so I'd really appreciate any help! 我已经为大脑和Google奋斗了很长时间,因此,我将非常感谢您的帮助!

Each task should have a date. 每个任务应有一个日期。 When app needs to query for the average per day/week/month/year you can use a NSPredicate to restrict only the results from a given time period. 当应用需要查询每天/每周/每月/每年的平均值时,您可以使用NSPredicate仅限制给定时间段内的结果。

For instance, here's a predicate that models the last-week scenario that I have written for an app of mine: 例如,这是一个谓词,用于模拟我为我的一个应用程序编写的上周场景:

  func lastWeekPredicate() -> NSPredicate {
    let calendar = NSCalendar.currentCalendar()
    let endDate = NSDate()
    let startDate = calendar.dateByAddingUnit(.CalendarUnitDay, value: -7, toDate: endDate, options: nil)
    return NSPredicate(format: "day >= %@ AND day =< %@", argumentArray: [startDate!, endDate!])
  }

Let's say you modelled your data with Core Data. 假设您使用Core Data建模了数据。 Querying with this predicate would give you the results of all the tasks that were completed in the last week. 使用该谓词查询将为您提供上周完成的所有任务的结果。 The predicate can be modified to work for days, months, year, or any specified number of days. 谓词可以修改为工作几天,几个月,一年或任何指定的天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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