ResearchKit - How to skip or add new questions to the survey while the survey is running?

196 Views Asked by At

I have three questions that should be asked if the user answered 2 or more "Yes". The survey currently have 12 questions, if the user answer yes to two or more questions, then I want to add 3 extra questions. (OR I will always add the 3 extra questions to my ORKOrderedTask, and then I would skip the last 3 questions is the user did not answer "Yes" to 2 or more questions).

I have been playing around with "taskViewController didChange" that lets me check if the current answer for the current question is "yes" or "no". I have a counter that keeps incrementing if the answer is "yes".

Now, I just need to either add 3 new questions if the counter reaches to 2. (OR skip the last 3 questions if counter never reaches to 2.)

SOLUTION

Instead of modifying the current survey, is easier to just create a new survey as soon as the first survey is completed. For me, I just had to check how many times the user answered "Yes". Therefore, I used this code in the "didFinish" (the function that gets executed when survey is completed) to check my answers:

    //If reason.rawValue is 2,then survey was successfully completed.
    if(reason.rawValue == 2){
      if let results = taskViewController.result.results as? [ORKStepResult] {
          for stepResult: ORKStepResult in results {
              for result in ([ORKResult]?(stepResult.results!))!{
                  if let questionResult = result as? ORKQuestionResult {
                      print("\(questionResult.answer!)") // This print each answer
                  }
              }
          }
      }
   }

Then, you do what you need to do with the answers and then you call:

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "IdentifierInYourStoryboard") taskViewController.present(viewController, animated: true, completion: nil)

Or make a segue or whatever transition method you wish to use. I used Storyboards.

0

There are 0 best solutions below