相关疑难解决方法(0)

防止NSURLSession缓存响应

为什么它会缓存响应.它返回先前获取的响应.它甚至可以在关闭网络连接时工作.重置iOS模拟器似乎也不起作用.提出请求,然后再次使用Internet脱机工作(缓存).

public let urlSession: NSURLSession

public init()
{
    // ...

    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    urlSession = NSURLSession(configuration: configuration)
}

func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
{
    let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
    let url = NSURL(string: urlString)
    let request = oauthInstance.request(forURL: url!)

    request.HTTPMethod = httpMethod
    self.customOAuth2Manager.setupRequest(request)

    for (key, value) in headers {
        request.setValue(value, forHTTPHeaderField:key)
    }

    if let body = body where body != "" {
        let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding) …
Run Code Online (Sandbox Code Playgroud)

caching nsurlcache nsurlsession nsurlsessionconfiguration swift

20
推荐指数
2
解决办法
1万
查看次数

iOS Swift - URLSessionDataTask 在刷新时无法获取更新的 JSON 数据

我有一个简单的应用程序,我以这种方式从存储在我自己的服务器中的 JSON 文件获取数据 - 我正在使用SwiftyJSON

func queryData(_ fileName:String) {
        guard let url = URL(string: JSON_PATH + fileName + ".json") else {return} // JSON_PATH + fileName + ".json" is the complete path to my db.json file (see below)
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let dataResponse = data, error == nil else {
                self.simpleAlert(error!.localizedDescription)
                return
            }
            let jsonData = try! JSON(data: dataResponse)
            print("ARRAY: \(jsonData)")
        }
        task.resume()
    }
Run Code Online (Sandbox Code Playgroud)



这是我的db.json文件:

[
    {
        "objID":"GNoW3vszYz",
        "string":"First …
Run Code Online (Sandbox Code Playgroud)

database json ios swift nsurlsessiondatatask

1
推荐指数
1
解决办法
2074
查看次数