When I send a GET request twice with Alamofire I get the same response but I'm expecting a different one. I was wondering if it was because of the cache, and if so I'd like to know how to disable it.
-
1For me none of the solutions below, or any that i found worked. Turned out, my hosting provider went through some changes and the JSON file was 'stuck' even though i would edit it, it always kept presenting the same file. Used another host and it worked. Probably few will have the same issue but it's worth noting. – Sam Bing Apr 13 '17 at 19:51
9 Answers
You have a few options.
Disabling the URLCache Completely
let manager: Manager = {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.URLCache = nil
return Manager(configuration: configuration)
}()
Configuring the Request Cache Policy
let manager: Manager = {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = .ReloadIgnoringLocalCacheData
return Manager(configuration: configuration)
}()
Both approaches should do the trick for you. For more information, I'd suggest reading through the documentation for NSURLSessionConfiguration and NSURLCache. Another great reference is NSHipster article on NSURLCache.

- 16,575
- 7
- 58
- 66
-
8
-
1@user3246173 see [Alamofire#manager](https://github.com/Alamofire/Alamofire#manager) – He Yifei 何一非 Apr 06 '16 at 15:17
-
1Doesn't work for me. And also after reading all content in the attached links I still don't know where to call this code. – Andrej Jul 26 '16 at 10:41
-
2At some point "Manager" became "SessionManager" and some other things changed. Swift 3 version: `let manager: SessionManager = { let configuration = URLSessionConfiguration.default configuration.requestCachePolicy = .reloadIgnoringLocalCacheData return SessionManager(configuration: configuration) }()` – snakeoil May 24 '17 at 20:55
This is what worked for me.
NSURLCache.sharedURLCache().removeAllCachedResponses()
Swift 3
URLCache.shared.removeAllCachedResponses()

- 1,251
- 16
- 17
-
1I can confirm this works. Calling the above code every time just before I make a network call. – Andrej Jul 26 '16 at 10:43
-
1@Andrej instead of that I just called it in the AppDelegate `didFinishLaunchWithOptions` – Venkatesh Chejarla Dec 27 '18 at 12:22
swift 3, alamofire 4
My solution was:
creating extension for 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")!))
}
}
}
and using it:
Alamofire.SessionManager.default
.requestWithoutCache("https://google.com/").response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
print("Error: \(response.error)")
}

- 2,256
- 1
- 17
- 14
-
Agree! Thank you. I've made a requestWithCache function too and now I simply use the proper one. I think this should be included in Alamofire by default – rmvz3 Apr 23 '17 at 00:08
-
-
1This doesn't seem to work for me. Are you sure this prevents reading from cache and not just prevents writing to cache? So if there is still something in cache you'll get that anyway? I'm experiencing caching problems with the iTunes lookup that I use to check if there is a new app version: [https://itunes.apple.com/nl/lookup/?id=1234567890](https://itunes.apple.com/nl/lookup/?id=1234567890). Clearing the cache with removeAllCachedResponses() does work though. – Janneman Jun 30 '17 at 08:48
-
Can you please help me how to enable cache up to 3 Minutes using Alamofire? – Nikunj Kumbhani Nov 12 '18 at 10:47
-
Cache lifetime can be set on server side in Cache-Control: max-age=. To find out, how to do same thing using Alamofire, create another question on stackoverflow. – 0x384c0 Nov 16 '18 at 06:16
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))
}
}

- 23,920
- 8
- 80
- 107
In Alamofire 4 and Swift 3:
// outside function, inside class
var sessionManager: SessionManager!
func someFunc() {
let configuration = URLSessionConfiguration.default
configuration.urlCache = nil
let sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.request("http://example.com/get").responseJSON { response in
// ...
}
}

- 2,592
- 4
- 38
- 69
-
For the comment above: https://github.com/Alamofire/Alamofire/issues/157 – Paul Slm Feb 07 '17 at 17:52
-
Same as https://stackoverflow.com/a/32230328/5790492 Better update answer than duplicate. – Nike Kov Mar 21 '19 at 13:14
[This approach doesn't disable caching, it merely makes sure that cached files aren't reused]
An easier way to get past cache problem for a particular call is to just add a random number in the call params.
For Swift 3, you can use, arc4random()
to generate a random number.

- 7,016
- 5
- 54
- 92
-
Yes, not a good approach to stop call caching, but a quick way to avoid cached data for a particular call. Useful for testing, obviously not a solution to get rid of caching as each of the call with a random number would still get cached. – Nagendra Rao Jun 27 '17 at 17:02
You can try to add cache-control to your headers.
let headers = ["Authorization": "Bearer \(token)",
"Cache-Control": "no-cache"]

- 8,021
- 7
- 61
- 120

- 73
- 6
Specifically removing a cached response before firing that request again would be more appropriate like:
let url = "http://google.com"
let urlRequest = URLRequest(url: URL(string: url)!)
URLCache.shared.removeCachedResponse(for: urlRequest)
Alamofire
.request(urlRequest)
.responseJSON(completionHandler: { response in
//handle response
}

- 63
- 5