Apple Health Frameworks

Nov 29 2022 Swift 5, iOS 15, Xcode 13

Part 3: Take Advantage of CareKit & ResearchKit

12. Extract OCKOutcomeValue from ORKTaskResult

Episode complete

Play next episode

Next
Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Make an ActiveTask Using ResearchKit. Next episode: 13. Create OCKDataSeriesConfiguration

This video Extract OCKOutcomeValue from ORKTaskResult was last updated on Nov 29 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Part 3, Episode 13, Extract OCKOutcomeValue from ORKTaskResult

In this episode, I want to show you how to store the ORKTaskResult by creating OCKOutcomeValue.

 static func checkInSurveyOutcome(_ result: ORKTaskResult) -> [OCKOutcomeValue]? {
  guard
    let response = result.results?
      .compactMap({ $0 as? ORKStepResult })
      .first(where: { $0.identifier == IdentifierModel.checkinForm.rawValue }),
    let scaleResults = response.results?
      .compactMap({ $0 as? ORKScaleQuestionResult }),
    let musclePainAnswer = scaleResults
      .first(where: { $0.identifier == IdentifierModel.checkinMuscle.rawValue })?
      .scaleAnswer,
    let headacheAnswer = scaleResults
      .first(where: { $0.identifier == IdentifierModel.checkinHeadache.rawValue })?
      .scaleAnswer,
    let tirednessAnswer = scaleResults
      .first(where: { $0.identifier == IdentifierModel.checkinTiredness.rawValue })?
      .scaleAnswer,
    let feverAnswer = scaleResults
      .first(where: { $0.identifier == IdentifierModel.checkinFever.rawValue })?
      .scaleAnswer,
    let nauseaAnswer = scaleResults
      .first(where: { $0.identifier == IdentifierModel.checkinNausea.rawValue })?
      .scaleAnswer
  else {
    assertionFailure("Failed to extract answers from check in survey!")
    return nil
  }

    var musclePainValue = OCKOutcomeValue(Double(truncating: musclePainAnswer))
    musclePainValue.kind = IdentifierModel.checkinMuscle.rawValue

    var headacheValue = OCKOutcomeValue(Double(truncating: headacheAnswer))
    headacheValue.kind = IdentifierModel.checkinHeadache.rawValue

    var tirednessValue = OCKOutcomeValue(Double(truncating: tirednessAnswer))
    tirednessValue.kind = IdentifierModel.checkinTiredness.rawValue

    var feverValue = OCKOutcomeValue(Double(truncating: feverAnswer))
    feverValue.kind = IdentifierModel.checkinFever.rawValue

    var nauseaValue = OCKOutcomeValue(Double(truncating: nauseaAnswer))
    nauseaValue.kind = IdentifierModel.checkinNausea.rawValue

    return [musclePainValue, headacheValue, tirednessValue, feverValue, nauseaValue]
  }
SurveyViewModel.checkInSurveyOutcome(_:)
 static func rangeOfMotionSurveyOutcome(_ result: ORKTaskResult) -> [OCKOutcomeValue]? {
    guard
      let motionResult = result.results?
        .compactMap({ $0 as? ORKStepResult })
        .compactMap({ $0.results })
        .flatMap({ $0 })
        .compactMap({ $0 as? ORKRangeOfMotionResult })
        .first else {
      assertionFailure("Failed to parse range of motion result")
      return nil
    }
    var range = OCKOutcomeValue(motionResult.range)
    range.kind = #keyPath(ORKRangeOfMotionResult.range)
    return [range]
  }

SurveyViewModel.rangeOfMotionSurveyOutcome(_:)