解析的JSON长时间未在api调用上更新

时间:2019-04-05 22:12:24

标签: json swift api

由于某种原因,从网络调用到我们构建的api后,解析的JSON对象不会更新。我检查了端点,现在发现事实立即更新。我有一个定时器,每10秒调用一次以进行呼叫,但解析的json直到一分钟左右才更新。我试图把它放在主线程上,那仍然行不通。这是我的代码:

@objc func getLaunches() {

    let simulator = UserDefaults.standard.string(forKey: self.launchSimulator)
    if(simulator == self.password){
        print("they are the same")
    }

    guard let launchUrl = URL(string: launchesURL) else {
        return
    }
    let request = URLRequest(url: launchUrl)
     DispatchQueue.main.async { [weak self] in
    let task = URLSession.shared.dataTask(with: request, completionHandler: {
        (data, response, error) -> Void in
        if let error = error {
            print(error)
            return
        }
        // Parse JSON data

        if let data = data {

            self?.launches.removeAll()
            self?.launches = (self!.parseJsonData(data: data))
            let nextlaunch = self?.launches[0]
            // Reload table view



                self?.hours = nextlaunch?.time
                self?.yearMonth = nextlaunch?.date
                var fulltime = self?.yearMonth

                fulltime!.insert("-", at: fulltime!.index(fulltime!.startIndex, offsetBy: 4))
                fulltime!.insert("-", at: fulltime!.index(fulltime!.startIndex, offsetBy: 7))
                fulltime = fulltime! + " "
                fulltime = fulltime! + self!.hours

                let fullFormatter = DateFormatter()
                fullFormatter.dateFormat = "YYYY-MM-dd HH:mm"
                fullFormatter.timeZone = TimeZone(abbreviation: "EST")
                self?.launchDate = fullFormatter.date(from: fulltime!)


                self?.getCountdown()


        }


    })
    task.resume()
    }

}


//parse launch info from json to dictionary into launches object
func parseJsonData(data: Data) -> [NextLaunch] {
    var launches = [NextLaunch]()

    do {
        let jsonResult = try JSONSerialization.jsonObject(with: data, options:
            JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary


        let jsonLaunches = jsonResult?["launches"] as! [NSDictionary]
        for jsonLaunch in jsonLaunches {
            let launch = NextLaunch()
            launch.date = jsonLaunch["date"] as! String
            launch.time = jsonLaunch["time"] as! String
            if(launch.time == ""){
                launch.time = "00:00"
            }
            launch.mission = jsonLaunch["mission"] as! String
            launch.launchpad = jsonLaunch["launch_pad"] as! String
            launch.image = jsonLaunch["image"] as! String
            launch.delay = jsonLaunch["delayed"] as! String

            //show delay image if it is delayed
            if(launch.delay == "1"){
                self.delayed()
            }else{
                self.notDelayed()
            }

            launches.append(launch)
        }
    } catch {
        print(error)
    }

    return launches

}

1 个答案:

答案 0 :(得分:0)

您需要

DispatchQueue.main.async {
   self?.getCountdown()
}

URLSession.shared.dataTask(with:的响应在后台线程中发生