简体   繁体   中英

URLSession datatask accessing older version of website?

I am using URLSession to scrape JSON data from my website. My code was throwing various errors relating to casting types, so I added some print statements to my code and found that this function is for some reason accessing an older version of my site. I have since updated the website's data, and verified that the new data is displaying properly both through visiting the website myself and using Rested. However, the print statements in the code below yield old data. The code does not read data from the disk so I am not sure why this is happening.

I have removed the website's link from my code for privacy purposes, but otherwise the function can be found below.

   func websiteToDisk() {

        let config = URLSessionConfiguration.default

        config.waitsForConnectivity = true

        let defaultSession = URLSession(configuration: config)

        let url = URL(string: someURL)

        let task = defaultSession.dataTask(with: url!) { data, response, error in

            do {

                print("Getting information from website")

                if let error = error {

                    print(error.localizedDescription)

                } else if let data = data,

                    let response = response as? HTTPURLResponse,

                    response.statusCode == 200 {

                    //do {

                    let jsonDecoder = JSONDecoder()

                    print("about to dcode")

                    let decodedData = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]//try jsonDecoder.decode([String: [String]].self, from: data)

                    print(decodedData)

                    print("accessing dictionary")

                    print(decodedData!["busLoops"])

                    let toWrite = decodedData!["busLoops"] as! [String]



                    let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

                    let busLoopsURL = documentDirectoryURL.appendingPathComponent("busLoops").appendingPathExtension("json")



                    let jsonEncoder = JSONEncoder()

                    let jsonData = try jsonEncoder.encode(toWrite)

                    try jsonData.write(to: busLoopsURL)

                    //} catch { print(error)}

                }

            }

            catch { print(error)}

        }

        task.resume()

    }

Try ignore local cache data

guard let url = URL(string: "http://....") else{
    return
}
let request = NSMutableURLRequest(url: url)
request.cachePolicy = .reloadIgnoringCacheData
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, resp, error) in

}
task.resume()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM