ios - Where do I specify ReloadIgnoringLocalCacheData for NSURLSession in Swift 2 -
i have app makes api call every 5 seconds using nsurlsession , p2-oauth2. i'm running issue of returning cached data instead of updated information api. read this post matt thompson describes different cache policies, 1 think need use reloadignoringlocalcachedata
. think it's suppose put in appdelegate didfinishlaunchingwithoptions functions. but, problem i'm having don't know or how specify it. haven't found swift solutions. can tell me function should say?
if it's helpful, here api request:
let urlpath = "https://sandbox-api.uber.com/v1/requests/\(uberrequestid)" let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate guard let endpoint = nsurl(string: urlpath) else { print("error creating endpoint");return } let request = appdelegate.oauth.request(forurl: nsurl(string:urlpath)!) request.setvalue("application/json; charset=utf-8", forhttpheaderfield: "content-type") request.httpmethod = "get" //get response uber , iterate through find uber product id. nsurlsession.sharedsession().datataskwithrequest(request) { (data, response, error) -> void in { guard let dat = data else { throw jsonerror.nodata } let result = try nsjsonserialization.jsonobjectwithdata(dat, options: nsjsonreadingoptions.mutablecontainers) print(result) //set status status = result["status"] as! string print("found status...returning -> \(status)") completion(status: "\(status)") } catch let error jsonerror { print(error.rawvalue) print("error needs handled.") } catch { print(error) print("error needs handled.") } }.resume()
here final request sets cache policy. added 1 line reloadignoringlocalcachedata
.
let urlpath = "https://sandbox-api.uber.com/v1/requests/\(uberrequestid)" let url:nsurl = nsurl(string: urlpath)! let session = nsurlsession.sharedsession() let appdelegate = uiapplication.sharedapplication().delegate as! appdelegate let request = appdelegate.oauth.request(forurl: nsurl(string:urlpath)!) request.httpmethod = "get" //added line set cache policy request.cachepolicy = nsurlrequestcachepolicy.reloadignoringlocalcachedata request.setvalue("application/json; charset=utf-8", forhttpheaderfield: "content-type") let task = session.datataskwithrequest(request) { ( let data, let response, let error) in guard let _:nsdata = data, let _:nsurlresponse = response error == nil else { print("error") return } let datastring = nsstring(data: data!, encoding: nsutf8stringencoding) print(datastring) } task.resume()
Comments
Post a Comment