活动指示器SwiftUI

时间:2019-11-07 05:16:04

标签: swift download boolean swiftui activity-indicator

我正在尝试找到一种解决方案,该方法如何使跑步和停止我的活动指示器。

我有一个活动指标,该指标已通过布尔值$ isLoading激活

ActivityIndicator(isAnimating:$isLoading,style: .large)

现在我的问题是.. 我有一个功能,可以从网上以xml格式下载一些数据并显示在屏幕上。 当我按下按钮(下载)时,我要运行我的功能(),并显示活动指示器,直到下载完成。 这是我的问题: 我如何理解我的数据何时下载完毕? 下载完成后如何将isLoading的状态从true更改为false。

我的下载功能:

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()

        }
    }

提前感谢您的帮助...

0 个答案:

没有答案