tylerSF February 2016

Where do I specify ReloadIgnoringLocalCacheData for NSURLSession in Swift 2

I have an app that makes an API call every 5 seconds using NSURLSession and p2-oauth2. I'm running into an issue of it returning cached data instead of the updated information from the API. I read this post by Matt Thompson where he describes the different cache policies, the one I think I need to use is ReloadIgnoringLocalCacheData. I think it's suppose to be put in the AppDelegate DidFinishLaunchingWithOptions functions. But, the problem I'm having is I don't know where or how to specify it. I haven't found any Swift solutions. Can anyone tell me what my function should say?

If it's helpful, here is my 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 from Uber and iterate through to find Uber Product ID.
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
        do {
            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 it back -> \(status)")
            completion(status: "\(status)")

        } catch let error as JSONError {
            print(error.rawValue)
            print("ERROR NEEDS TO BE HANDLED.")
        } catch {
            print(erro        

Answers


dgatwood February 2016

Assuming the OAuth API returns a mutable request, you can set its cachePolicy property to NSURLRequestCachePolicy.ReloadIgnoringCacheData.


tylerSF February 2016

Here is the final request that properly sets the cache policy. I added one line with 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 this line to 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  where error == nil else {
        print("error")
        return
    }

    let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
    print(dataString)

}

task.resume()

Post Status

Asked in February 2016
Viewed 1,254 times
Voted 11
Answered 2 times

Search




Leave an answer