具有异步功能的DispatchGroup

时间:2018-10-03 11:50:21

标签: ios swift

我使用Alamofire来获取请求。我有两个UIViewControllers,我使用prepare(segue)函数在两者之间发送数据。

在我的第一个视图控制器上,我使用Alamofire,但是当我使用prepare (segue)时,我所有的信息都是空的。

 @IBAction func loginPage(_ sender: UIButton) {
    let group = DispatchGroup()
    Helper().alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: paramaters) { contenuJSON in
        if (contenuJSON["connected"].stringValue == "true") {
            group.enter()
            self.dashboad()
            group.leave()
            group.notify(queue: DispatchQueue.main) {
                //print(self.image) // EMPTY
                print(self.info[0]) // EMPTY FATAL ERROR INDEXT OUT OF RANGE
                self.performSegue(withIdentifier: "Dashboard", sender: self)
            }
        }
    }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Dashboard" {
        let success = segue.destination as! DashboardViewController
        success.profil = self.image
    }
}
func dashboad() {
     // Other Function 
    //self.image = addPicProfil()
    self.info = add_info(url: "http://192.168.1.7/app_dev.php/dashboard/info")
}
func add_info(url: String) -> [String] {
    var info = [String]()
    Helper().alomofireGet(URL: url) { contentJSON in
        var content = contentJSON
        print(content)
        info.append(contentJSON["userFirstName"].stringValue)
        info.append(contentJSON["countDevices"].stringValue)
        info.append(contentJSON["earnedThisYearsEUR"].stringValue)
        info.append(contentJSON["countCampaigns"].stringValue)
    }
    return (info)
}

在我的助手文件中,我有:

    func alomofireGet(URL: String, onCompletion:@escaping ((JSON) -> Void)) {
    var contentJSON = JSON()
    Alamofire.request(URL, method: .get).responseJSON() { (reponse) in
        if reponse.result.isSuccess {
            contentJSON = JSON(reponse.result.value!)
        } else {
            contentJSON = JSON(reponse.result.error!)
        }
        onCompletion(contentJSON)
    }
}
func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>, onCompletion: @escaping ((_ response: JSON) -> Void)) {
    Alamofire.request(URL, method: .post, parameters: Paramaters).validate().responseJSON { (reponse) in
        var contenuJSON = JSON()
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        onCompletion(contenuJSON)
    }
}

1 个答案:

答案 0 :(得分:0)

您认为DispatchQueue info毫无用处

self.info = add_info(url: "http://192.168.1.7/app_dev.php/dashboard/info")

将添加附加的异步值,但它将返回一个空数组,您需要

func add_info(url: String,completion:@escaping(_ arr:[String]) -> ()) {
    var info = [String]()
    Helper().alomofireGet(URL: url) { contentJSON in
        print(contentJSON)
        info.append(contentJSON["userFirstName"].stringValue)
        info.append(contentJSON["countDevices"].stringValue)
        info.append(contentJSON["earnedThisYearsEUR"].stringValue)
        info.append(contentJSON["countCampaigns"].stringValue)
        completion(info)
    }
}