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:
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. 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!
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.
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.