CareKit and ResearchKit conflict

122 Views Asked by At

I have a problem with types of results form surveys. If I use CareKit / HealthKit surveys, everything is fine, when I want to mix it with ResearchKit surveys - I have error in method below:

func buildCarePlanResultFrom(taskResult: ORKTaskResult) -> OCKCarePlanEventResult {
    guard let firstResult = taskResult.firstResult as? ORKStepResult,
        let stepResult = firstResult.results?.first else {
            fatalError("Unexepected task results")
    }
    if let numericResult = stepResult as? ORKNumericQuestionResult,
        let answer = numericResult.numericAnswer {
        return OCKCarePlanEventResult(valueString: answer.stringValue, unitString: numericResult.unit, userInfo: nil)
    } else {

    }

    fatalError("Unexpected task result type")
}

Which is - I think - because I cannot make a OCKCarePlanEventResult object [answer and numericResult are both nil].

I update symptoms tracker in extension below:

extension TabBarViewController: ORKTaskViewControllerDelegate {
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith
    reason: ORKTaskViewControllerFinishReason, error: Error?) {
    defer {
        dismiss(animated: true, completion: nil)
    }
    guard reason == .completed else { return }
    guard let symptomTrackerViewController = symptomTrackerViewController,
        let event = symptomTrackerViewController.lastSelectedAssessmentEvent else { return }
    symptomTrackerViewController.progressRingTintColor = .lightGreen
    symptomTrackerViewController.delegate = self
    let carePlanResult = carePlanStoreManager.buildCarePlanResultFrom(taskResult: taskViewController.result)
    carePlanStoreManager.store.update(event, with: carePlanResult, state: .completed) {
        success, _, error in
        if !success {
            print(error?.localizedDescription)
        }
    }
}}

Any suggestions how convert type of results from ResearchKit to CareKit or other solution to update the view?

May, this will be helpful, example of task:

    let sleepHoursSurveyActivity = OCKCarePlanActivity.assessment(
        withIdentifier: ActivityIdentifier.sleepHours.rawValue,
        groupIdentifier: nil,
        title: "Sleep Hours",
        text: nil,
        tintColor: .darkGreen,
        resultResettable: true,
        schedule: dailyScheduleRepeating(occurencesPerDay: 1),
        userInfo: ["ORKTask" : AssessmentTaskFactory.sleepHoursSurveyAssessmentTask()])

static func sleepHoursSurveyAssessmentTask() -> ORKTask {
    var steps = [ORKQuestionStep]()
    let question = NSLocalizedString("How many hours did you sleep?", comment: "")
    let textChoices = [
        ORKTextChoice(text: "1-3", value: "1-3" as NSString),
        ORKTextChoice(text: "4-5", value: "4-5" as NSString),
        ORKTextChoice(text: "6-7", value: "6-7" as NSString),
        ORKTextChoice(text: "8", value: "8" as NSString),
        ORKTextChoice(text: "more than 8", value: "more than 8" as NSString)
        ]

    let questAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
    let sleepHoursSurvaySteps = ORKQuestionStep(identifier: "sleepHours", title: question, answer: questAnswerFormat)
    sleepHoursSurvaySteps.isOptional = false
    steps += [sleepHoursSurvaySteps]

    return ORKOrderedTask(identifier: "sleepHoursSurveyTask", steps: [sleepHoursSurvaySteps])
}
1

There are 1 best solutions below

1
On

Your function sleepHoursSurveyAssessmentTask which returns a task doesn't have a numeric answer format. You are using an ORKTextChoiceAnswerFormat which you can extract from ORKTaskResult using ORKChoiceQuestionResult.