Rutger Huijsmans
Rutger Huijsmans

Reputation: 2408

API call using Alamofire won't refresh

The first time I call this API it works fine. The second time I call an API using this code however it seems like nothing happens. The data returned is exactly the same and the backend doesn't register any incoming API calls from my client.

It seems like some sort of caching is happening and I've no idea how to turn it off. Using Alamofire 4.5.

This is my code:

func getPopularBrocodes(_ offset: Int, completionHandler: @escaping (([AnyObject]?, Int?, AnyObject?) -> Void)) {
    let urlString = Constant.apiUrl().appendingFormat(Constant.apiGetPopularBrocodes).appending("?offset=\(offset)")

    Alamofire.request(urlString, method: .get, encoding: Alamofire.JSONEncoding.default, headers: self.header).validate().responseJSON(completionHandler: {response in
        if response.result.isSuccess {
            if let value = response.value {
                var jsonString = ""
                let json = JSON(value)
                jsonString = json["data"].rawString()!
                let nextInt = Int(json["next"].rawString()!)
                if let brocodes:Array<BroCode> = Mapper<BroCode>().mapArray(JSONString: jsonString) {
                    completionHandler(brocodes, nextInt, nil)
                } else {
                    completionHandler(nil, nextInt, nil)
                }
            }
        }
        if response.result.isFailure{

        }
    })
}

Upvotes: 0

Views: 312

Answers (1)

Gerrit Post
Gerrit Post

Reputation: 1267

Yes, local cache is the problem. There are multiple solutions.

How to disable caching in Alamofire

You could confirm that it is local caching by removing all the cached responses. Just add this: URLCache.shared.removeAllCachedResponses()

Upvotes: 1

Related Questions