0

Currently I am building an app using research kit. Below is my question.

let onsetQuestionStepTitle = "Title"
let onsetQuestionStepQuestion = "Question Name"
let onsetTextChoices = [
    ORKTextChoice(text: "superb", value: 0 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "great", value: 1 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "fair", value: 2 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "not good", value: 3 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "bad", value: 4 as NSCoding & NSCopying & NSObjectProtocol)
]
let onsetAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: onsetTextChoices)
let onsetQuestionStep = ORKQuestionStep(identifier: "twoAwakeSurvey", title: onsetQuestionStepTitle, question: onsetQuestionStepQuestion, answer: onsetAnswerFormat)
steps += [onsetQuestionStep]

If the user taps on superb I want to skip to the next step with an identifier "threeAwakeSurvey" and go to "fourAwakeSurvey." To do that I implemented the below code.

var task = ORKNavigableOrderedTask(identifier: "StepNavigationTaskIdentifier", steps: steps)
let resultSelector: ORKResultSelector = ORKResultSelector(resultIdentifier: "twoAwakeSurvey")
let askLocation = ORKResultPredicate.predicateForChoiceQuestionResult(with: resultSelector, expectedAnswerValue: 0 as NSCoding & NSCopying & NSObjectProtocol)

let locationNavigationRule = ORKPredicateSkipStepNavigationRule(resultPredicate: askLocation)
task.setSkip(locationNavigationRule, forStepIdentifier: "threeAwakeSurvey")

However this is not working. I read the documentation but still don't get why.

Satsuki
  • 2,166
  • 5
  • 17
  • 33

1 Answers1

1

I tried below code in the playground and its working. Maybe your step 3 identifier is incorrect.

let onsetQuestionStepTitle = "Title"
let onsetQuestionStepQuestion = "Question Name"
let onsetTextChoices = [
    ORKTextChoice(text: "superb", value: 0 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "great", value: 1 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "fair", value: 2 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "not good", value: 3 as NSCoding & NSCopying & NSObjectProtocol),
    ORKTextChoice(text: "bad", value: 4 as NSCoding & NSCopying & NSObjectProtocol)
]
let onsetAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: onsetTextChoices)
let onsetQuestionStep = ORKQuestionStep(identifier: "twoAwakeSurvey", title: onsetQuestionStepTitle, question: onsetQuestionStepQuestion, answer: onsetAnswerFormat)

let threeAwakeSurveyStep = ORKInstructionStep(identifier: "threeAwakeSurvey")
threeAwakeSurveyStep.title = "Three"

let fourAwakeSurveyStep = ORKInstructionStep(identifier: "fourAwakeSurvey")
fourAwakeSurveyStep.title = "Four"

let steps = [onsetQuestionStep, threeAwakeSurveyStep, fourAwakeSurveyStep]

var task = ORKNavigableOrderedTask(identifier: "StepNavigationTaskIdentifier", steps: steps)
let resultSelector: ORKResultSelector = ORKResultSelector(resultIdentifier: "twoAwakeSurvey")
let askLocation = ORKResultPredicate.predicateForChoiceQuestionResult(with: resultSelector, expectedAnswerValue: 0 as NSCoding & NSCopying & NSObjectProtocol)

let locationNavigationRule = ORKPredicateSkipStepNavigationRule(resultPredicate: askLocation)
task.setSkip(locationNavigationRule, forStepIdentifier: "threeAwakeSurvey")

let taskVC = ORKTaskViewController(task: task, taskRun: nil)

PlaygroundPage.current.liveView = taskVC
Arpit
  • 111
  • 6
  • Hello thank your for the comment where should I use ORKTaskViewController(task: task, taskRun: nil)? I am not doing anything in the ORKTaskViewControllerDelegate. – Satsuki Jul 01 '18 at 07:21
  • You can present ORKTaskViewController on any UIViewController in your application. For example, `@IBAction func openTask(sender: UIButton) { viewController.present(taskVC, animated: true) }` – Arpit Jul 01 '18 at 17:51