1

我对结果表调查的类型有疑问。如果我使用 CareKit / HealthKit 调查,一切都很好,当我想将它与 ResearchKit 调查混合时 - 我在以下方法中有错误:

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")
}

这是 - 我认为 - 因为我无法制作 OCKCarePlanEventResult 对象 [answer 和 numericResult 都是 nil]。

我在下面的扩展中更新了症状跟踪器:

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)
        }
    }
}}

有什么建议如何将 ResearchKit 的结果类型转换为 CareKit 或其他解决方案以更新视图?

可能,这会有所帮助,任务示例:

    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])
}
4

1 回答 1

0

sleepHoursSurveyAssessmentTask返回任务的函数没有数字答案格式。您正在使用ORKTextChoiceAnswerFormat可以从ORKTaskResultusing中提取的ORKChoiceQuestionResult.

于 2018-06-17T01:43:53.287 回答