swift中的Apple CareKit症状代码不会将数据保存到CareStore

时间:2016-11-04 21:33:33

标签: ios swift researchkit carekit

以下代码部分正常运行。它显示问题步骤和表单。用户能够输入数据,但是当用户点击完成后,没有任何内容可以保存到护理店并且显示不会更新。有什么想法吗?

class SymptomsVC1: UIViewController{


fileprivate let carePlanStoreManager = CarePlanStoreManager1.sharedCarePlanStoreManager
fileprivate let carePlanData: CarePlanData
fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil



required init?(coder aDecoder: NSCoder) {
    carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)

    super.init(coder: aDecoder)

}



override func viewDidLoad() {
    super.viewDidLoad()
    setViewControllerTitle(self, title: "Symptoms Card")
    //creatMenuObject(self)


    let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store)
    symptomTracker.progressRingTintColor = UIColor.magenta
    symptomTracker.delegate = self
    symptomTracker.showEdgeIndicators = true
    // Setup the controller's title
    symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "")

    //change left navigation "Back" button to menu button
    var backImage:UIImage = UIImage(named: "menu")!
    backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
    symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton;

    self.navigationController?.pushViewController(symptomTracker, animated: true)


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


extension SymptomsVC1: OCKSymptomTrackerViewControllerDelegate {

  func symptomTrackerViewController(_ viewController:    OCKSymptomTrackerViewController, didSelectRowWithAssessmentEvent assessmentEvent: OCKCarePlanEvent) {

if viewController.progressRingTintColor == UIColor.magenta {

    guard let userInfo = assessmentEvent.activity.userInfo,
        let task: ORKTask = userInfo["ORKTask"] as? ORKTask else { return }

    let taskViewController = ORKTaskViewController(task: task, taskRun: nil)
    taskViewController.delegate = self

    present(taskViewController, animated: true, completion: nil)


     }
  }
}

extension SymptomsVC1: ORKTaskViewControllerDelegate {

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith
reason: ORKTaskViewControllerFinishReason, error: Error?) {

defer {
    dismiss(animated: true, completion: nil)
}
print("task view controller clicked")
guard reason == .completed else { return }
guard let symptomTrackerViewController = symptomTrackerViewController,
      let event = symptomTrackerViewController.lastSelectedAssessmentEvent else { return }

let carePlanResult = carePlanStoreManager.buildCarePlanResultFrom(taskResult: taskViewController.result)
print("care plan result")
print(carePlanResult)
carePlanStoreManager.store.update(event, with: carePlanResult, state: .completed) {
    success, _, error in
    if !success {
        print(error?.localizedDescription)
    }
  }
 }
}

1 个答案:

答案 0 :(得分:0)

我通过在以下函数中添加一行代码找到了修复程序:

override func viewDidLoad() {
super.viewDidLoad()
setViewControllerTitle(self, title: "Symptoms Card")
//creatMenuObject(self)


let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store)
symptomTracker.progressRingTintColor = UIColor.magenta
symptomTracker.delegate = self
symptomTracker.showEdgeIndicators = true
// Setup the controller's title
symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "")

//*** add the following line, now the result show up in carestore ***
symptomTrackerViewController = symptomTracker 


//change left navigation "Back" button to menu button
var backImage:UIImage = UIImage(named: "menu")!
backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton;

self.navigationController?.pushViewController(symptomTracker, animated: true)

}