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

问题描述 投票:0回答:2

我想运行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之前运行)

swift alamofire
2个回答
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!

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 _{}
})
© www.soinside.com 2019 - 2024. All rights reserved.