如何理解何时下载数据?
下面的函数使用Alamofire下载一些数据,并使用SwiftyXML对其进行解析。
我的问题是我需要了解下载何时完成才能停用活动指示器。
func getXML (airportICAO: String,aptName : String,aptCity : String,aptCountry: String) {
let downloadData = DataModel(aptICAO: airportICAO,aptName: aptName,aptCity: aptCity,aptCountry: aptCountry,aptMetaR: "",aptTAF: "",aptWindDir: 0,aptWindSpeed: 0,aptMetarplain: "",metarCondition: "",metarVisibility: "",metarTep: "",metarDewp: 0,metarqNH: 0,metarTendency: "")
downloadData.aptICAO = airportICAO
request( "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=\(airportICAO)").responseData { respond in
if let datareceive = respond.data {
let xml = XML.parse(datareceive)
let metar = xml.response.data.MetaR.raw_text.text
downloadData.aptMetaR = metar ?? "NIL MetaR"
print(metar ?? "NIL")
let flight_category = xml.response.data.MetaR.flight_category.text
downloadData.metarCondition = flight_category ?? "NIL FLIGHT CAT"
print(flight_category ?? "NIL")
let temp_c = xml.response.data.MetaR.temp_c.double
let roundedT = String(format: "%.0f",temp_c ?? 000)
downloadData.metarTep = roundedT
let wind_dir_degrees = xml.response.data.MetaR.wind_dir_degrees.int
downloadData.aptWindDir = wind_dir_degrees ?? 000
let wind_speed_kt = xml.response.data.MetaR.wind_speed_kt.int
downloadData.aptWindSpeed = wind_speed_kt ?? 000
let observation_time = xml.response.data.MetaR.wind_speed_kt.text
downloadData.aptMetarplain = observation_time ?? "NIL NO observation time provided"
let altim_in_hg = xml.response.data.MetaR.altim_in_hg.double
let t = altim_in_hg ?? 000
let v = t * 33.8639
downloadData.metarqNH = Int(v.rounded())
let dewpoint_c = xml.response.data.MetaR.dewpoint_c.int
downloadData.metarDewp = dewpoint_c ?? 000
let visibility_statute_mi = xml.response.data.MetaR.visibility_statute_mi.double
let roundVis = String(format: "%.1f",visibility_statute_mi ?? 000)
downloadData.metarVisibility = roundVis
}
}
//request for the taf
request( "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&hoursBeforeNow=48&timeType=issue&mostRecent=true&stationString=\(airportICAO)").responseData { respond in
if let data = respond.data {
let dataReceive = XML.parse(data)
let taf = dataReceive.response.data.TAF.raw_text.text
downloadData.aptTAF = taf ?? "NIL TAF"
print(taf ?? "NIL")
}
self.storage.insert(downloadData,at: 0)
self.salva()
}
}