Sptibo
Sptibo

Reputation: 283

Alamofire, Updated data on server is not updating on response

Here is my alamofire code.

let headers: HTTPHeaders = ["Accept": "application/json",
                                "Authorization": Setting.getLoginStatus().accessToken]
    let url = "http://thedemo.net/demo/stdinaus/api/jobs-near-me?page=1&latitude=27.6947033&longitude=85.3310636"

Alamofire.request(url, method: .get, encoding : JSONEncoding.default, headers: headers).responseJSON { (response) in
        guard let jobsResponse = response.result.value as? [String:Any] else{
            print("Error: \(String(describing: response.result.error))")
            failure((response.result.error! as Error))
            return
        }
            print("response: \(jobsResponse)") }

Response is coming properly and displayed on related screen. I've call this from viewDidLoad() in view controller. When data is updated on server the new data doesn't appear on the response. But if I delete the app and run again (i.e. if app is running for the first time) then the updated data is coming properly.

Is that a problem with alamofire code? Please someone help me with this issue. Thanks in advance

Upvotes: 1

Views: 1660

Answers (2)

Rocky
Rocky

Reputation: 3235

Can be issue with cache data, so ignore it when you calling the API.

let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = . reloadIgnoringLocalAndRemoteCacheData

var req = URLRequest(url: URL(string: YOUR_API_URL)!)
req.httpMethod = "GET"
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.setValue("application/json", forHTTPHeaderField: "Accept")
req.setValue(Setting.getLoginStatus().accessToken, forHTTPHeaderField:"Authorization" )
req.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData

Alamofire.request(req).validate().responseJSON { (response) in
        print(response)
    }

Upvotes: 1

John Doe
John Doe

Reputation: 185

so if I understand correctly, you want the data in your app to be updated as soon as when it is updated in your server?

If this is the case, you shouldn't be calling it in viewDidLoad() method because this method is only called once when your view loads. It wont be called again until you restart your app.

Upvotes: 0

Related Questions