首页 > 解决方案 > 了解何时下载数据

问题描述

我如何了解数据何时下载?

下面的函数使用 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()
    }
}

标签: swiftfunctiondownloadreturn

解决方案


推荐阅读