Alamofire loading from cache even when cache policy set to ReloadIgnoringLocalAndRemoteCacheData

前端 未结 3 1794
难免孤独
难免孤独 2021-01-01 22:30

I set cache policy to request in Alamofire to ignore local cache.

Then I load a viewcontroller with network connection, then I disconnect network connection, kill th

相关标签:
3条回答
  • 2021-01-01 22:51

    Thanks to @FábioSalata I solved my problem like this.

     var req = URLRequest(url: URL(string: "<URL>")!)
     req.httpMethod = "GET"
     req.setValue("application/json", forHTTPHeaderField: "Content-Type")
     req.setValue("<Auth KEY>", forHTTPHeaderField:"Authorization" )
     req.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
    
     Alamofire.request(req).validate().responseJSON { response in ...
    
    0 讨论(0)
  • 2021-01-01 22:55

    ReloadIgnoringLocalAndRemoteCacheData is not implemented.

    https://developer.apple.com/reference/foundation/nsurlrequestcachepolicy/1408722-reloadignoringlocalandremotecach

    http://nshipster.com/nsurlcache/

    Update: Starting with iOS 13, NSURLRequest.CachePolicy.reloadRevalidatingCacheData and NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData are implemented. https://developer.apple.com/documentation/ios_ipados_release_notes/ios_13_release_notes

    0 讨论(0)
  • 2021-01-01 23:03

    I'm using this way in a project and it's working:

    let mutableURLRequest = NSMutableURLRequest(URL: SERVICEURL)
    mutableURLRequest.HTTPMethod = "POST"
    
    mutableURLRequest.HTTPBody = self.createJson()
    mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    mutableURLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
    
    request(mutableURLRequest).validate().responseJSON{ response in...
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题