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
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Make an ActiveTask Using ResearchKit. Next episode: 13. Create OCKDataSeriesConfiguration

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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(_:)