简体   繁体   English

Swift 4 - Alamofire - 如何根据请求禁用缓存

[英]Swift 4 - Alamofire - How to disable cache on request

I have this Alamofire request like so:我有这样的 Alamofire 请求:

func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
    {

        let user = username

        let password = password

        let url = webservice

        let credential = URLCredential(user: user, password: password, persistence: .none)

        let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]

        Alamofire.request(url, method: .get, headers: headers).authenticate(usingCredential: credential).responseJSON {
                (response) in

                print(response.result)

                switch response.result {

                case .success:
                    if let value = response.result.value {

                        completion(true)

                    }else{

                        print("There is error in the server response")

                        completion(false)
                    }

                case .failure (let error):

                    print("The NTLM request error is: ", error.localizedDescription)

                    completion(false)

                }

            }

    }

My problem with it is it works when I use invalid credentials after using valid credentials, instead of spitting out an error.我的问题是当我在使用有效凭据后使用无效凭据时它会起作用,而不是吐出错误。 I have been told this maybe a caching issue, so my question is how do I disable cache for this request?有人告诉我这可能是缓存问题,所以我的问题是如何禁用此请求的缓存?

I have tried adding this to the top of my request:我曾尝试将此添加到我的请求的顶部:

URLCache.shared.removeAllCachedResponses()

But that didn't work但这没有用

I also saw this:我也看到了这个:

urlRequest.cachePolicy = .reloadIgnoringCacheData

But I have no idea where to apply it.但我不知道在哪里应用它。

Please help!请帮忙!

UPDATE更新

I have tried the following:我尝试了以下方法:

func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
    {

        let user = username

        let password = password

        let url = webservice

        let credential = URLCredential(user: user, password: password, persistence: .none)

        var urlRequest = URLRequest(url: URL(string: url)!)

        urlRequest.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData

        urlRequest.httpMethod = "get"

        urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Content-type")

        urlRequest.setValue("application/json;odata=verbose", forHTTPHeaderField: "Accept")

        Alamofire.request(urlRequest).authenticate(usingCredential: credential).responseJSON {
                (response) in

                switch response.result {

                case .success:
                    if let value = response.result.value {


                       completion(true)

                    }else{

                        print("There is error in the server response")

                        completion(false)
                    }

                case .failure (let error):

                    print("The NTLM request error is: ", error.localizedDescription)

                    completion(false)

                }

            }

    }

But that did not work, same result.但这不起作用,结果相同。

You can instantiate a Alamofire.SessionManager with a specific URLSessionConfiguration .您可以实例化一个Alamofire.SessionManager与特定URLSessionConfiguration In the configuration you can set the cachePolicy.在配置中你可以设置cachePolicy。 This could look like this:这可能是这样的:

let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .useProtocolCachePolicy // or whatever you want
configuration.urlCache = nil
let mySessionManager = Alamofire.SessionManager(configuration: configuration)

and then use it as follows:然后按如下方式使用它:

mySessionManager.request(urlRequest) .... mySessionManager.request(urlRequest) ....

Note that you have to use mySessionManager to do requests instead of using Alamofire.request if you follow this.请注意,如果您遵循此操作,则必须使用mySessionManager来执行请求,而不是使用Alamofire.request

To make a request using mySessionManager it is basically the same instead of Alamofire.request(urlRequest) ... you use mySessionManager.request(urlRequest) ...要使用 mySessionManager 发出请求,它基本上是相同的,而不是Alamofire.request(urlRequest) ...你使用mySessionManager.request(urlRequest) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM