简体   繁体   中英

Swift, How to add func in a block

Currently, I am working on loading data on a expandable tableview. The problem is that, the loading of tableview is on ViewDidLoad(). And I have write a func to load data from server.The main thread is used for loading tableview and the second thread is running for collecting data from server. It means that when the tableview is loaded, it cannot set the tableview with data I retrieved from server.Also, I tried print out the value on jsonResponse, it was correct. While the value in ConfigTable() was incorrect. Any idea? Thanks

//keep specific symptons
  var npcPatient = npc_participant()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    fetchData(patientID)
    configTable()
    // 1 for default expand, 0 for collapse
    arrayForBool = ["1","0","0"]
    sectionTitleArray = ["DETAILS for PATIENTS","ENROLMENT INFORMATION","GENETIC DIAGNOSIS"]
    var string1 = sectionTitleArray .objectAtIndex(0) as? String
    sectionContentDict.setValue(self.DicPatientInfo, forKey: string1!)
    string1 = sectionTitleArray.objectAtIndex(1) as? String
    sectionContentDict.setValue(self.DicEnrollInfo, forKey:string1! )
    string1 = sectionTitleArray.objectAtIndex(2) as? String
    sectionContentDict.setValue(self.DicPatientInfo, forKey: string1!)

    self.myTable.registerNib(UINib(nibName: "ExpandingTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")

}

 func fetchData(id:Int){
    let cn = connection()
    let id = self.current_patient.id
    let url = "http://localhost:3000/api/v1/patients/"+String(id)
    print(url)
    // Call function to connect server by API with specific url
    cn.connectServer(url) { (jsonResponse) in
        //Parsing JSON file
    for item in jsonResponse["patients"].arrayValue{
        //get user info
    self.npcPatient.baby_symptoms = item["enrollable"]["baby_symptoms"].intValue 
    }}

  func configTable(){
    // Configue List1
  let name = current_patient.registrant_first_name + " "+current_patient.registrant_last_name
    let dob = current_patient.date_of_birth
    var gender = ""
    if(current_patient.gender == 0){
         gender = "male"
    }else{
         gender = "female"
    }
    self.DicPatientInfo.setValue(name, forKey: "PatientName")
    self.DicPatientInfo.setValue(dob, forKey: "Date_of_Birth")
    self.DicPatientInfo.setValue(gender, forKey: "Gender")    
    //config List2
    self.DicEnrollInfo.setValue(self.npcPatient.baby_symptoms,     forKey: "Did you have prolonged jaundice, liver problems, or an enlarged liver and/or spleen as a baby?")
      print(self.npcPatient.baby_symptoms)

}
typealias Completion = (success:Bool) -> Void

    func fetchData(id:Int,completionHandler: Completion) {
        let cn = connection()
        let id = self.current_patient.id
        let url = "http://localhost:3000/api/v1/patients/"+String(id)
        print(url)s
        // Call function to connect server by API with specific url
        cn.connectServer(url) { (jsonResponse) in
              //Parsing JSON file
              if let JSON = jsonResponse {
                  for item in JSON["patients"].arrayValue {
                      //get user info
                     self.npcPatient.baby_symptoms = item["enrollable"]["baby_symptoms"].intValue 
                  }
                  completionHandler(success:true)
              } else {
                  completionHandler(success:false)
              }
        }
    }

To use it in your case:

fetchData(patientID, { (success) -> Void in
    // When download completes,control flow goes here.
    if success {
        // download success
        configTable()
        self.myTable.reloadData() 
    } else {
        // download fail
        print("Download problem..")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM