Open fedetop01 opened 1 year ago
You might want to consider using the HealthKit linked tasks in CareKit. The task can be scheduled to occur every day, and will pull data from HealthKit to populate its outcome. The scheduling logic might look something like this:
func createCalorieTask() -> OCKHealthKitTask {
let calorieLinkage = OCKHealthKitLinkage(
quantityIdentifier: .activeEnergyBurned,
quantityType: .cumulative,
unit: .kilocalorie()
)
let startOfDay = Calendar.current.startOfDay(for: Date())
let dailySchedule = OCKScheduleElement(
start: startOfDay,
end: nil,
interval: DateComponents(day: 1),
duration: .allDay,
targetValues: [OCKOutcomeValue(300)]
)
let calorieTask = OCKHealthKitTask(
id: "calories",
title: "Burn Calories",
carePlanUUID: nil,
schedule: OCKSchedule(composing: [dailySchedule]),
healthKitLinkage: calorieLinkage
)
return calorieTask
}
Now in terms of scheduling a notification, you can choose to either schedule or "unschedule" that 8PM notification whenever the outcome for the task changes.
var query = OCKEventQuery(for: anIntervalEncompassingAFewEvents)
query.taskIDs = ["calories"]
for events in store.anyEvents(matching: query) {
// schedule or unschedule notifications based on the
// progress for each event
}
Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs
I have problem with the schedule of this task you provided, and I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day
Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs
Which branch of CareKit are you using? The main branch should have that property.
I have problem with the schedule of this task you provided
What problem are you running into?
I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day
You can begin by checking the progress for an event. If the progress is not completed, schedule a notification for 8PM. If the progress is completed, remove the 8PM notification.
`func createCalorieTask() -> OCKHealthKitTask {
let calorieLinkage = OCKHealthKitLinkage(
quantityIdentifier: .activeEnergyBurned,
quantityType: .cumulative,
unit: .kilocalorie()
)
let startOfDay = Calendar.current.startOfDay(for: Date())
let dailySchedule = OCKScheduleElement(
start: startOfDay,
end: nil,
interval: DateComponents(day: 1),
duration: .allDay,
targetValues: [OCKOutcomeValue(300)]
)
let calorieTask = OCKHealthKitTask(
id: "calories",
title: "Burn Calories",
carePlanUUID: nil,
schedule: OCKSchedule(composing: [dailySchedule]),
healthKitLinkage: calorieLinkage
)
return calorieTask
}` this task should be added to the healthkitstore right? And how do I check the progress to schedule the notification? and the OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem, too
Which branch of CareKit are you using? The main branch should have that property. it must be 2.1 version
this task should be added to the healthkitstore right?
Yep! The OCKHealthKitPassthroughStore
or an OCKStoreCoordinator
.
And how do I check the progress to schedule the notification?
On 2.1, fetch the event for the task and check to see if the outcome value matches the target value for the schedule (which looks to be 300 calories based on the snippet above). To check compare those two, see OCKAnyEvent.outcome
and OCKAnyEvent.scheduleEvent
OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem
That initializer expects a single date, not a date interval. You can use this initializer instead - OCKEventQuery(dateInterval:)
Hi, I was trying to schedule a notification based on the calorie burned during the day by the app user, so that only if the user had burned during the day less than 300 kcal (for example) by 8 in the evening, it would have scheduled the notification and sent it by that hour, otherwise it would have done nothing. However, I have lots of difficulties managing the schedule of the notification everyday only if the condition is met, which should be verified with query to the health store. and that's where things get worse, since the query is asynchronous. here is a snippet of the code In tried so far, but it stop on the first day, and never check the condition again, nor schedule the notification: `// MARK: NOTIFICATION BASED ON HEALTHKIT DATA func scheduleNotificationIfNeeded() { let dispatchGroup = DispatchGroup() let center = UNUserNotificationCenter.current() let content = UNMutableNotificationContent() content.title = "Reminder" content.body = "You haven't burned enough calories today. Is that because you are experiencing severe pain? Come log it in!" content.sound = .default