2017-12-22 16 views
0

以前のAlamofireリクエストの結果をパラメータとして使用して、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前に実行させるための方法を探しています)IMOがDispatchGroupを使用することです

答えて

1

最も簡単な方法は、あなたが詳細を読むことができますここで: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をスキップ>グループに入っているため、このソリューションは動作しませんでした>実行しますgroup.wait()は永遠に待っています。 – Gintal

+0

使用したコードを正確に表示できますか?私は実際に私の自己を実行し、私はうまく動作することを見たコードで私の例を更新しました。 – netigger

+0

今、うまく働いています、どうもありがとうございます。 "DispatchQueue.global(qos:.userInitiated).async"が見つかりませんでした。 – Gintal

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 _{} 
}) 
関連する問題