ios swift ResearchKit condition for skip with NSPredicate



我正在使用ResearchKit开发一个小应用程序。不幸的是,我找不到足够的示例来弄清楚如何根据上一个问题的答案跳过问题。我已经将以下代码放在一起,我知道它不行且不起作用。我正在寻求帮助以找出什么是不行的。

let textChoices2 = [
ORKTextChoice(text: "Answer 1", value: 0),
ORKTextChoice(text: "Answer 2", value: 1),
ORKTextChoice(text: "Answer 3", value: 2),
ORKTextChoice(text: "Answer 4", value: 3)
]
let questAnswerFormat2: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormatWithStyle(.SingleChoice, textChoices: textChoices2)
let questQuestionStep2 = ORKQuestionStep(identifier: "TextChoiceQuestionStep2", title: questQuestionStepTitle, answer: questAnswerFormat2)
//////////////////////////// Here I need help
let task: ORKNavigableOrderedTask = ORKNavigableOrderedTask(identifier: "xyz123", steps: steps)
//the identifier for ORKNavigableOrderedTask is a random number in my case, is it ok like this?
let resultSelector: ORKResultSelector = ORKResultSelector(stepIdentifier: "TextChoiceQuestionStep", resultIdentifier: "ImageChoiceQuestionStep”)
//stepIdentifier is the question based on which I decide if I want to skip the current question. Is it ok like this?
//resultIdentifier is the question to which I want to jump to. Is it ok like this?
let predicate: NSPredicate = ORKResultPredicate.predicateForChoiceQuestionResultWithResultSelector(resultSelector, expectedAnswerValue: "0”)
//expectedAnswerValue is the value of the answer for which I want to skip the current question. Is it ok like this?
let predicateRule: ORKPredicateStepNavigationRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers:[ (predicate, "ImageChoiceQuestionStep") ])
//“ImageChoiceQuestionStep” from the row above is the question to which I want to jump to. Is it ok like this?
task.setNavigationRule(predicateRule, forTriggerStepIdentifier: "ImageChoiceQuestionStep”)
//“ImageChoiceQuestionStep” from the row above is the question to which I want to jump to. Is it ok like this?
questQuestionStep2.task = task

resultSelector (docs) 的功能是标识您希望谓词匹配的确切结果。resultSelector必须有一个resultIdentifier,在有问题的步骤中,它既是问题步骤的标识符,也是其中的问题结果(它不是您要跳转到的步骤的标识符!

(可选)resultSelector可以具有stepIdentifier(当您要匹配表单结果时,可以在同一步骤中包含多个表单结果)或taskIdentifier(如果要匹配以前完成的任务中的结果)。我认为您的用例不需要这两个。

然后,使用指向您感兴趣的问题的有效resultSelector,构建一个结果谓词,并使用谓词构建一个具有destinationStepIdentifierORKPredicateStepNavigationRule(这是您要跳转到的步骤)。

最后,将谓词步骤导航规则附加到必须触发该规则的步骤(该步骤可以是谓词匹配的同一步骤,也可以是任务后面显示的步骤)。

设置完所有内容并运行任务后,当您离开导航规则附加到的步骤时,将触发导航规则,并且导航规则中包含的 destinationIdentifier 指向您有条件跳转到的步骤。

有关如何在 swift 上实现条件跳转的完整示例,请参阅 TaskFactory.swift 文件(该文件在 ORKTest 示例项目中实现了可导航有序任务项)。

另请查看有关ORKNavigableOrderedTaskORKPredicateStepNavigationRuleORKResultPredicate的文档以获取更多信息。

我相信你可以通过使用这些来实现你想要的。


此外,还有一个正在进行的拉取请求,它将跳过导航规则的概念添加到ORKNavigableTask,这可能更适合您的需求。但我建议你先掌握一般导航规则的工作原理。希望PR将很快合并。

相关内容

  • 没有找到相关文章

最新更新