ios - How long does URLSession dataTask cache my server's data? -
this first app , i'm wondering if have made mistake regards using urlsession.shared datatask because not seeing app new data updated. see new json instantly in browser when refresh, but, app apparently not see it.
will ever new json data server without uninstalling app?
there some similar question topics, such how disable caching nsurlsessiontask however, not want disable caching. instead, want know how default object behaves in scenario - how long going cache it? if indeed answer forever, or until update or reinstall app, want know how reproduce normal browser based cache behavior, using if-modified-since header, not question here.
i call download() function below gratuitously after launch sequence.
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 // call datatask , see ch , calls ch let task = session.datatask(with: mytesturl!) { (data, response, error) // generic ch datatask in // special ch ch(data,response,error) // make sure file gets written in ch } task.resume() // no such thing status in async here }
within completion handler pass download, save data code "ch":
dispatchqueue.main.async { let documentcontroller = uidocumentinteractioncontroller.init(url: myfileurl!) documentcontroller.delegate = self uidocumentinteractioncontrollerdelegate }
and finally, read data within same completion handler disk such:
let data = try data(contentsof: myfileurl!)
for clarification, complete calling function call download() completion handler code.
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") { try data!.write(to: myfileurl!) dispatchqueue.main.async { let documentcontroller = uidocumentinteractioncontroller.init(url: myfileurl!) documentcontroller.delegate = self 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 , saved file successfully") } else { let alertcontroller = uialertcontroller(title: "get_tests", message: "failed 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) } }) }
Comments
Post a Comment