在完成另一个Alamofire请求后运行Alamofire请求

时间:2017-12-22 17:37:18

标签: swift alamofire

我想运行Alamofire请求,该请求使用先前Alamofire请求的结果作为参数。简单来说:

    //Code1
    Alamofire.request("URL", method: HTTPMethod.post, parameters: ["id": id as NSString)], encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
        let json = response.data
        do {
            print("This should be First")
            let data = try JSON(data: json!)
            let alllastmessages = data["Messages"].arrayValue
            for  i in 0..<alllastmessages.count {
                self.List.append(alllastmessages[i]["message"].stringValue)
            }}
        catch _{}
    })
    //End Code1

    //Code2
    print("This should be Last")
    for  i in 0..<List.count {
        Alamofire.request("URL2", method: .post , parameters: ["id": id as NSString] , encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
            //Do Something
        })
        self.tableView.reloadData()
    }
    //End Code2

(此代码已简化,我只想找到一种方法让Code1在Code2之前运行)

2 个答案:

答案 0 :(得分:2)

IMO最简单的方法是使用DispatchGroup,您可以在此处详细了解:https://developer.apple.com/documentation/dispatch/dispatchgroup

以下是一些适合我的代码:

    DispatchQueue.global(qos: .userInitiated).async {
        let group = DispatchGroup()
        group.enter()

        print("\(Date()) Start request 1!")
        Alamofire.request("https://github.com/CosmicMind/Material",
                          method: .get ,
                          parameters: [:] , encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
                            print("\(Date()) Request 1 completed!")
                            group.leave()
                          })

        group.wait()

        print("\(Date()) Start request 2!")
        Alamofire.request("https://github.com/CosmicMind/Material",
                          method: .get ,
                          parameters: [:] , encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
                            print("\(Date()) Request 2 completed!")
                          })
    }

打印:

2017-12-22 18:24:45 +0000 Start request 1!
2017-12-22 18:24:47 +0000 Request 1 completed!
2017-12-22 18:24:47 +0000 Start request 2!
2017-12-22 18:24:49 +0000 Request 2 completed!

答案 1 :(得分:0)

最简单的方法就是嵌套你的电话。您可以在第一个回调中调用第二个调用,如下所示:

Alamofire.request("URL", method: HTTPMethod.post, parameters: ["id": id as NSString)], encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
    let json = response.data
    do {
        print("This should be First")
        let data = try JSON(data: json!)
        let alllastmessages = data["Messages"].arrayValue
        for  i in 0..<alllastmessages.count {
            self.List.append(alllastmessages[i]["message"].stringValue)
        }
        //End Code1

        //Code2    
        print("This should be Last")
        for  i in 0..<List.count {
            Alamofire.request("URL2", method: .post , parameters: ["id": id as NSString] , encoding: URLEncoding.httpBody).response(completionHandler: { (response) in
                //Do Something
            })
            self.tableView.reloadData()
        }
    //End Code2
    } catch _{}
})