简体   繁体   English

嵌套的Alamofire请求

[英]Nested Alamofire Requests

I am making a nested Alamofire requests and populating my class property objects with the returned data. 我正在嵌套Alamofire请求,并使用返回的数据填充类属性对象。 The first Alamofire request works and I am able to inject data to my class object in the first Alamofire request, but the second request, although I see data returned from my API, I am unable to append the data to my class property which is an array if cities. 第一个Alamofire请求有效,我能够在第一个Alamofire请求中将数据注入类对象,但是第二个请求,尽管我看到了API返回的数据,但无法将数据附加到类属性中。如果城市数组。

 // Get States
getStatesByUserID(userID) {
    (result)->() in
    var states = JSON(result)
    for index in 0... states.count - 1 {
        let statesID: Int = states[index]["stateID"].intValue
        let statesName: String = states[index]["title"].stringValue


        // Get Cities by State ID
        self.getCities(stateID) {
            (result) -> () in
            let cities = JSON(result)
            for index in 0...cities.count - 1 {
                let cityID: Int = cities[index]["cityID"].intValue
                let cityName: String = cities[index]["title"].stringValue

                //Append a single city into cities array
                self.city.append(City(id: cityID, name: cityName))
            }
        }

        // Append a single state that contains cities and zips into states array
        self.state.append(State(id: stateID, name: stateName, city:self.city ))
    }
}
self.tableView.reloadData()
}

Alamofire works asynchronously, if your second request depends of the first one and your trying to append your data in the same callback probably your asking for data that hasn't been retrieved yet. Alamofire是异步工作的,如果您的第二个请求取决于第一个请求,并且您尝试将数据追加到同一回调中,则可能是您要查询尚未检索到的数据。 if this is the case you could await until the first request finish his job for example: 如果是这种情况,您可以等到第一个请求完成他的工作,例如:

   public func SendPicAndSig(ip: String, enerImg: EnerImage, callBack:((JSONData:JSON)->Void)?){
    let address:String = "http://\(ip)/Enercard.Service/api/UploadFiles"

    let serial =  JSONSerializer.toJson(enerImg)
    let _parametros = JSONSerializer.toDictionary(serial) as! [String : AnyObject]

    debugPrint(_parametros)
    Alamofire.request(.POST,address, parameters: _parametros, encoding:.JSON)
        .responseJSON { _,_,result in
            switch result
            {
            case .Success(let data):
                //posible handling de error de parte del servidor
                debugPrint(result.data)
                  callBack?(JSONData: JSON(data))
            default:()
                break
            }
    }
}

here i have an example function that I use to send and object and I have CallBack used as a parameters, this callback method will run only when the data is successfully retrieved so I could later call this function as 在这里,我有一个用于发送和对象的示例函数,并将CallBack用作参数,此回调方法仅在成功检索数据后才运行,因此以后可以将该函数调用为

SendPicAndSif("10.11.1.217" , Image("random-path"),SecondRequest)

where "SecondRequest" is going to be the function that i will run if I retrieve the data of the first request so 如果我检索第一个请求的数据,那么“ SecondRequest”将是我将运行的功能

SecondRequest(jsonarray:JSON)
{
  //second  Alamofire request
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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