首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析的JSON很长一段时间没有更新api调用

解析的JSON很长一段时间没有更新api调用
EN

Stack Overflow用户
提问于 2019-04-06 06:12:24
回答 1查看 19关注 0票数 0

由于某些原因,解析得到的JSON对象在网络调用和我们构建的api之后不会更新。我检查了端点,现在它会立即更新。我有一个计时器,每10秒调用一次,但解析的json直到大约一分钟后才更新。我试着把它放在主线上,但仍然不起作用。下面是我的代码:

代码语言:javascript
运行
复制
@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

}
EN

回答 1

Stack Overflow用户

发布于 2019-04-06 06:24:05

你需要

代码语言:javascript
运行
复制
DispatchQueue.main.async {
   self?.getCountdown()
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55544001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档