如何禁用Alamofire中的缓存

Rém*_*zak 43 caching ios swift alamofire

当我用Alamofire两次发送GET请求时,我得到相同的响应,但我期待一个不同的响应.我想知道是否是因为缓存,如果是这样,我想知道如何禁用它.

cno*_*oon 42

你有几个选择.

完全禁用URLCache

let manager: Manager = {
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.URLCache = nil
    return Manager(configuration: configuration)
}()
Run Code Online (Sandbox Code Playgroud)

配置请求缓存策略

let manager: Manager = {
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.requestCachePolicy = .ReloadIgnoringLocalCacheData
    return Manager(configuration: configuration)
}()
Run Code Online (Sandbox Code Playgroud)

这两种方法都应该适合你.有关更多信息,我建议您阅读NSURLSessionConfigurationNSURLCache的文档.另一个很好的参考是关于NSURLCache的 NSHipster文章.

  • 这段代码应该放在哪里?在app委托?请详细说明. (7认同)
  • 在某些时候,"经理"成为"SessionManager",其他一些事情也发生了变化.Swift 3版本:`let manager:SessionManager = {let configuration = URLSessionConfiguration.default configuration.requestCachePolicy = .reloadIgnoringLocalCacheData return SessionManager(configuration:configuration)}()` (2认同)

All*_*las 38

这对我有用.

NSURLCache.sharedURLCache().removeAllCachedResponses()
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

URLCache.shared.removeAllCachedResponses()
Run Code Online (Sandbox Code Playgroud)


And*_*rew 30

迅捷3,alamofire 4

我的解决方案是:

为Alamofire创建扩展:

extension Alamofire.SessionManager{
    @discardableResult
    open func requestWithoutCache(
        _ url: URLConvertible,
        method: HTTPMethod = .get,
        parameters: Parameters? = nil,
        encoding: ParameterEncoding = URLEncoding.default,
        headers: HTTPHeaders? = nil)// also you can add URLRequest.CachePolicy here as parameter
        -> DataRequest
    {
        do {
            var urlRequest = try URLRequest(url: url, method: method, headers: headers)
            urlRequest.cachePolicy = .reloadIgnoringCacheData // <<== Cache disabled
            let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
            return request(encodedURLRequest)
        } catch {
            // TODO: find a better way to handle error
            print(error)
            return request(URLRequest(url: URL(string: "http://example.com/wrong_request")!))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用它:

Alamofire.SessionManager.default
            .requestWithoutCache("https://google.com/").response { response in
                print("Request: \(response.request)")
                print("Response: \(response.response)")
                print("Error: \(response.error)")
        }
Run Code Online (Sandbox Code Playgroud)


War*_*shi 8

func getImage(url: String, completion: @escaping (UIImage?) -> ()) {

    let urlRequest = URLRequest(url: URL(string: url)!)
    URLCache.shared.removeCachedResponse(for: urlRequest)
    //URLCache.shared.removeAllCachedResponses()

    Alamofire.request(url).responseData { (dataResponse) in
        guard let data = dataResponse.data else {
            return completion(nil)
        }
        completion(UIImage(data: data, scale:1))
    }
}
Run Code Online (Sandbox Code Playgroud)