这是我的第一个应用程序,我想知道我是否在使用URLSession.shared dataTask方面犯了一个错误,因为我没有看到我的应用程序获得经常更新的新数据。我刷新时会立即在浏览器中看到新的json,但是,我的应用程序显然看不到它。
它是否会在不卸载应用程序的情况下从我的服务器获取新的json数据?
有一些类似的问题主题,例如How to disable caching from NSURLSessionTask但是,我不想禁用所有缓存。相反,我想知道这个默认对象在这种情况下的行为 - 缓存它有多长时间?如果确实答案是永远的,或者直到他们更新或重新安装应用程序,那么我将想知道如何使用if-modified-since标头重现正常的基于浏览器的缓存行为,但这不是我的问题。
在启动序列之后,我无偿地调用我的download()函数。
func download(_ ch: @escaping (_ data: Data?, _ respone: URLResponse?, _ error: Error?) -> (), completionHandler: @escaping (_ sessionError: Error?) -> ()) {
let myFileURL: URL? = getFileURL(filename: self.getFilename(self.jsonTestName))
let myTestURL = URL(string:getURLString(jsonTestName))
let session = URLSession.shared
// now we call dataTask and we see a CH and it calls My CH
let task = session.dataTask(with: myTestURL!) { (data, response, error) // generic CH for dataTask
in
// my special CH
ch(data,response,error) // make sure the file gets written in this ch
}
task.resume() // no such thing as status in async here
}
在我传递下载的完成处理程序中,我使用以下代码保存数据" ch":
DispatchQueue.main.async {
let documentController = UIDocumentInteractionController.init(url: myFileURL!)
documentController.delegate = self as UIDocumentInteractionControllerDelegate
}
然后最后,我从磁盘中读取同一个完成处理程序中的数据:
let data = try Data(contentsOf: myFileURL!)
为了澄清,我的完整调用函数从中调用了带有完成处理程序代码的download()。
func get_test(){ // download new tests
let t = testOrganizer
let myFileURL: URL? = t.getFileURL(filename:t.getFilename(t.jsonTestName))
t.download( { (data,response,error)
in
var status: Int! = 0
status = (response as? HTTPURLResponse)?.statusCode
if(status == nil) {
status = 0
}
if(error != nil || (status != 200 && status != 304)) {
let alertController = UIAlertController(title: "Error downloading", message:"Could not download updated test data. HTTP Status: \(status!)", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
self.p.print("END OF COMPLETION HANDLER")
}
else {
let status = (response as! HTTPURLResponse).statusCode
print("Success: status = ", status)
self.p.print("WRITING FILE IN COMPLETION HANDLER")
do {
try data!.write(to: myFileURL!)
DispatchQueue.main.async {
let documentController = UIDocumentInteractionController.init(url: myFileURL!)
documentController.delegate = self as UIDocumentInteractionControllerDelegate
}
} catch {
// // _ = completionHandler(NSError(domain:"Write failed", code:190, userInfo:nil))
print("error writing file \(myFileURL!) : \(error)")
}
self.myJson = self.testOrganizer.readJson()
self.p.print("END OF COMPLETION HANDLER")
}
}, completionHandler: {
sessionError in
if(sessionError == nil) {
print("Downloaded and saved file successfully")
} else {
let alertController = UIAlertController(title: "get_tests", message:
"Failed to download new tests - " + sessionError.debugDescription, preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
}
})
}