Creating Schedules for Tasks
Create schedules for tasks using multiple schedule elements.
Overview
When you define a schedule for performing a task, you also define one or more schedule elements. These elements allow you to build up complex behaviors composed from multiple steps.
Schedules are based around specific dates, times and intervals. Reduce the potential for complexity in calculating these, by using the Calendar API and the Date Components API wherever possible. These tools will handle edge cases, such as daylight savings times, and leap years that can otherwise introduce inaccuracies when attempting to calculate using seconds-based time intervals.
Compose Schedule Elements
For example, you can see that a series of reference objects are created as a starting point for a schedule, based on the returned value from initializing a Date object:
// A date that represents 00h 00m 00s today or midnight.
let startOfDay = Calendar.current.startOfDay(for: Date())
// A date that represents 08h 00m 00s today or 8am.
let breakfastTime = Calendar.current.date(byAdding: .hour, value: 8, to: startOfDay)!
// A date that represents 13h 00m 00s or 1pm.
let lunchTime = Calendar.current.date(byAdding: .hour, value: 5, to: breakfastTime)!
// A date that represents 19h 30m 00s or 7.30pm.
let dinnerTime = Calendar.current.date(byAdding: .minute, value: 270, to: lunchTime)!Using these reference dates, you can build a schedule for a task that should be completed at 8 a.m. on day 1, 1 p.m. on day 2, and 7:30 p.m. on day 3:
// A date component for offsetting by 3 days
let threeDays = DateComponents(day: 3)
var day1 = OCKScheduleElement(start: breakfastTime, end: nil, interval: threeDays)
var day2 = OCKScheduleElement(start: Calendar.current.date(byAdding: .day, value: 1, to: lunchTime)!, end: nil, interval: threeDays)
var day3 = OCKScheduleElement(start: Calendar.current.date(byAdding: .day, value: 2, to: dinnerTime)!, end: nil, interval: threeDays)
let multiDaySchedule = OCKSchedule(composing: [day1, day2, day3])Add Context to Schedule Elements
You add context to each element of the schedule by setting the text property with details of the time the element represents, and specifying a duration that the task should take:
var element = OCKScheduleElement(start: breakfastTime, end: nil, interval: DateComponents(day: 1))
element.text = "Every day at 8am for 5 minutes"
element.duration = 300Include Outcome Values
Each time a user completes a task event in your app, the data store saves the outcome. This outcome can contain a value or values which you configure in the schedule element. When your app presents the task for a given day, it uses the schedule defined by the elements to determine what values to associate with the outcome.
You create outcome values with an OCKOutcomeValue. An OCKOutcomeValue is created with a value, which can be an Int, Double, Bool, String, Data or Date, and a unit which is a String. Because an instance of a task might have multiple dimensions, for example, 2 tablets each of 500mg, you can specify an array of them for each schedule element:
var tablet = OCKOutcomeValue(25.00, units: "mg")
var dosage = OCKOutcomeValue(2, units: "tablets")
element.targetValues = [tablet, dosage]