carekit-apple / CareKit

CareKit is an open source software framework for creating apps that help people better understand and manage their health.
https://www.researchandcare.org
Other
2.39k stars 441 forks source link

Write to OCKHealthKitTask? #569

Open advatar opened 3 years ago

advatar commented 3 years ago

I am interested in logging cups of water directly into HealthKit so I created this task:

let water = OCKHealthKitTask(
            id: "water",
            title: "Water",
            carePlanUUID: nil,
            schedule: .anytime,
            healthKitLinkage: OCKHealthKitLinkage(
                quantityIdentifier: .dietaryWater,
                quantityType: .cumulative,
                unit: .cupUS()))

When I try to log using:

let drinkLogger = OCKButtonLogTaskViewController(task: drinksOfWaterTask, eventQuery: .init(for: date),
                                                                    storeManager: self.storeManager)
                    listViewController.appendViewController(drinkLogger, animated: false)

I get this error:

16:37:34.224 ❤️ ERROR CareViewController.taskViewController():81 - invalidValue(reason: "Failed to fetch: No object OCKCDTask for UUID 5B7E0C2D-4FED-4F32-B54B-B02DA6DB7198")

What should I do to be able to update HealthKit linked entities? I guess a workaround could be to let the error happen and update HK in the handler but there must be a better way?

gavirawson-apple commented 3 years ago

Good question! We currently do not support modifying HealthKit data from CareKit, only reading data. There are some intricacies involved, mainly the restriction of modifying data in HealthKit that wasn't created by the CareKit app. But this is a feature we're working towards, so hang in there!