Skip to content

Conversation

@Rodrigox30
Copy link

@Rodrigox30 Rodrigox30 commented Aug 6, 2024

Currently, when querying OCKHealthKitTask from the CareStore, the captured dates of samples are not accessible by developers from their respective OCKOutcomeValues. Adding this information to OCKOutcomeValue will allow developers to differentiate when samples were taken to provide insights into trends over time. The current problem is the createdDate can only be accessed on an OCKOutcomeValue which for a HealthKit sample is incorrect because the createdDate is the date the sample was queried from HealthKit (see here) as opposed to the actual start/end dates of the HealthKit sample. To reflect HealthKit samples startDate and endDate in OCKOutcomeValues, OCKOutcomeValue.createdDate is now set to the start date of the sample, and the newly added endDate is set to the end date of the sample.

These are the proposed changes to CareKit:

  • Updated CareKitStore2.1 to CareKitStore3.0 by adding endDate attributes to OCKCDOutcomeValue
  • Reflected these changes in OCKCDOutcomeValue.swift and OCKOutcomeValue.swift (this allows the endDate to be accessed for HealthKit samples but also allows developers to store/retrieve their own dates for these values)
  • Converting a sample to an outcome value and storing it on the event's outcome now has endDate sample values, with startDate sample values stored in createdDate
  • Test to confirm that these changes were made
  • Provided migration for CareKitStore2.0->CareKtStore3.0 and CareKitStore2.1->CareKtStore3.0

@gavirawson-apple please let me know if you want me to make any additional updates

Collaborators: @cbaker6

Rodrigox30 and others added 3 commits August 6, 2024 10:05
* feat: date acesses for created date to end date to task outcomes

* endDate documentation update

* Updated OCKOutcomeValue startDate and endDate documentation to HealthKit documentation.

* Added 2.0 to 3.0 CoreData mapping and adjusted OCKOutcomeValue members

* nits

* Test added for Outcome Value

* CareKitStore3.0 mapping update

* Succesful migration from CareKitStore 2.1 to CareKitStore 3.0.

* Add file to Package.swift

* add files to Xcode project

* fix coredata in project

* nits

* fix absolute path for coredata

* deleted files

---------

Co-authored-by: Corey Baker <[email protected]>
@Rodrigox30 Rodrigox30 marked this pull request as draft August 6, 2024 17:43
@Rodrigox30 Rodrigox30 marked this pull request as ready for review August 6, 2024 19:40
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This CareKitStore only adds two new attributes to OCKCDOutcomeValue, startDate and endDate, to CareKitStore 2.1. Nothing else has changed from the 2.1 store

Copy link
Author

@Rodrigox30 Rodrigox30 Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same custom migration policy from CareKitStore 2.0 to CareKitStore 2.1 updated to migrate CKS 2.0 to CKS 3.0. The only change that occurred was updating the schema version to 3.0.0.


// Update the schema version to 3.0
if key == "schemaVersion" {
dInstance.setValue("3.0.0", forKey: key)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated schema version

Copy link
Author

@Rodrigox30 Rodrigox30 Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping is one-to-one for CKS 2.1 to CKS 3.0. I need to hardcode the schemaVersion to 3.0.0 since $source.schemaVersion would not be correct, and I do not want to introduce a new custom migration policy for one change.

Changes occurred to:

  • OCKCDOutcomeToOCKCDOutcome
  • OCKCDCarePlanToOCKCDCarePlan
  • OCKCDTaskToOCKCDTask
  • OCKCDContactToOCKCDContact
  • OCKCDPatientToOCKCDPatient

@NSManaged var units: String?
@NSManaged var createdDate: Date
@NSManaged var startDate: Date?
@NSManaged var endDate: Date?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added startDate and endDate

)

outcomeValue.startDate = sample.dateInterval.start
outcomeValue.endDate = sample.dateInterval.end
Copy link
Author

@Rodrigox30 Rodrigox30 Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we add the sample's startDate and endDate to its OCKOutcomeValue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files were added to test migration from CareKitStore2.1 to CareKitStore3.0 (used in testMigrationFrom2_1to3_0)

/// Outcomes: 3
/// OutcomeValues: 3
func testMigrationFrom2_0to2_1() throws {
func testMigrationFrom2_0to3_0() throws {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed testMigrationFrom2_0to2_1 because we are not using CareKitStore 2.1. Created new tests for the previous stores to check if their migration was successfull.

Copy link
Author

@Rodrigox30 Rodrigox30 Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of how this updated OCKOutcomeValue allows developers to store/retrieve their own dates for these values

XCTAssertEqual(outcomeValues.count, 2)
XCTAssertEqual(outcomeValues.first?.doubleValue, 70)
XCTAssertEqual(outcomeValues.first?.startDate, heartRateStart)
XCTAssertEqual(outcomeValues.first?.endDate, heartRateEnd)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test to check if the sample's startDate and endDate were successfully saved in their OCKOutcomeValue


/// The value's end date.
public var endDate: Date?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added startDate and endDate to OCKOutcomeValue

@Rodrigox30 Rodrigox30 changed the title feat: Add startDate and endDate to OCKOutcomeValue feat: Add endDate to OCKOutcomeValue May 10, 2025
@cbaker6
Copy link
Contributor

cbaker6 commented May 15, 2025

@gavirawson-apple @aplummer-apple if this ends up getting merged after a successful review (🤞🏾) we plan to open up another PR that enables OCKOutcomeValue's to represent HKQuantity and HKCategory samples that consist of all valid HK properties in the Care Store and on OCKOutcomeValue. This will also allow CareKit to access HKCategory types using linkage:

public struct OCKHealthKitLinkage: Equatable, Codable {
    /// Initialize by specifying HealthKit types.
    ///
    /// - Parameter categoryIdentifier: A HealthKitCategoryIdentifier that describes the outcome's data type.
    public init(categoryIdentifier: HKCategoryTypeIdentifier) {
        self.sampleIdentifier = categoryIdentifier.rawValue
    }
}

For details on the working implementation see: cbaker6#23, cbaker6@79c88ab, cbaker6#26

HKQuantity and HKCategory samples can then be queried, graphed, saved to the OCKStore, etc. over any date interval using #723

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants