Overview: I am developing my first application. It collects user location information for tracking a bicycle ride. I save the contents to a .txt file (coordinate information and attribute information) The user can then export their data out of the iOS application for further analysis. I have successfully added a save text file function to store the information mentioned above along with the ability to export that file outside of the application.
Problem: I have set up a ResearchKit survey to collect additional attribute information that can be tied to a users bike ride. Sample questions include, why a user is riding/trip purpose and what is the weather, etc.). I would like to append the results of the ResearchKit survey to the text file that has the coordinate information, or create a separate text file if necessary. I know I need to somehow access the ORKStepResult task, but I am unfamiliar with the process.
Research: I have searched Github for sample apps using researchKit, but the results are very limited for apps written in Swift. I am not a comfortable enough "developer" to be able to read Objective-C and then translate that into Swift... One day! I have also looked at tutorials from Rey Wenderlich, but they don't mention how to access, save and export your survey results. I have looked at other questions on StackExchange, but most with detailed answers pertain to Objective-C.
Thank you StackExchange your help would be greatly appreciated!
Basically, you can access the result tree (which has an ORKTaskResult
obect as a root) by implementing the func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?)
method in the ORKTaskViewController
delegate and accessing taskViewController.result
from there.
Then, recursively iterate over ORKTaskResult
's results
property until you reach the leaves, and you can access and store the desired information in your format of choice.
See the ORKResult.h
file for the documentation of the different result types.
If you just print the root ORKTaskResult
to the console (or to a text file), it will recursively show you all the result tree, although I think that would not a be completely suitable format as these show object names and memory addresses which you don't need.
Finally, you may want to have a look to the ORKCatalog
example project in the official repository. When you run the app, it has a Results tab which shows the results for the survey or activity that was run last.
The code that reads and displays the results is in the Displaying Results
group on the ORKCatalog
project. This is the aforementioned task did finish delegate method, which in turns sets the results in the results view controller as shown here, and which ultimately gets processed by the code in the ResultTableViewProviders.swift
file.
However, the result processing code here is a little bit involved (as it accounts for all the different result types) and probably your solution would be much much simpler if you only want to process results of a specific type.