菜鸟教程小白 发表于 2022-12-11 17:09:32

ios - NSURLCache Alamofire 删除缓存的响应


                                            <p><p>我正在使用以下代码来测试 <code>NSURLCache</code> 中的行为。我在 <code>AppDelegate</code> 中初始化了一个 <code>API</code> 实例。我根据 Alamofire 的文档配置管理器,配置共享缓存,并分配 <code>dataTaskWillCacheResponse</code> 以确保响应确实会被缓存。</p>
<p>然后我调用 <code>makeRequest</code> 来检查缓存的响应是否存在(它不应该在第一次启动时),然后我使用我的 <code>manager</code> 发出请求相同的 URL,以便请求在整个测试过程中是等效的。</p>
<p>我在 <code>dataTaskWillCacheResponse</code> 的断点被命中,我继续,<code>responseJSON</code>block 被执行并且是 <code>Success</code>ful 所以我 <code>performTests</code> 使用请求。</p>
<ol>
<li>首先,我检查响应是否被缓存。 <strong>很好!</strong></li>
<li>第二,<strong>(这就是问题所在)</strong>我删除了该请求的缓存响应,然后检查它是否存在。 <strong>确实如此:糟糕!</strong></li>
<li>第三,我检查删除 <em>all</em> 缓存的响应是否会删除该响应。 <strong>确实:好! <em>但奇怪的是,这行得通,而之前尝试仅删除单个响应并没有...</em></strong></li>
</ol>
<h3>代码如下:</h3>
<pre><code>import Alamofire

class API: Manager.SessionDelegate {
   
    var manager: Manager!

    override init() {
      super.init()
      manager = Manager(session: urlSession(), delegate: self)
      configureCache(memoryCapacityMB: 5, diskCapacityMB: 25)
      manager.delegate.dataTaskWillCacheResponse = { urlSession, dataTask, cachedResponse in
            // Placing a breakpoint here confirms that the response is going to be cached
            return cachedResponse
      }
    }
   
    private func urlSession() -&gt; NSURLSession {
      let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
      return NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    }
   
    private func configureCache(memoryCapacityMB memory: Int, diskCapacityMB disk: Int) {
      let memoryCapacity = memory * 1024 * 1024
      let diskCapacity = disk * 1024 * 1024
      let sharedCache = NSURLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: nil)
      NSURLCache.setSharedURLCache(sharedCache)
    }
   
    // MARK: Request
   
    func makeRequest() {
      // The response should be nil on the first launch since nothing has been cached
      let request = NSURLRequest(URL: NSURL(string: &#34;http://jsonplaceholder.typicode.com/posts&#34;)!)
      let response = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
      print(response)
      
      manager.request(.GET, request.URLString).responseJSON { response in
            switch response.result {
            case .Success:
                self.performTests(with: response.request!)
   
            case .Failure:
                break
               
            }
      }
    }
   
    func performTests(with request: NSURLRequest) {
      // Should exist
      var response = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
      print(response)
      // And it does: good!
      
      // Remove the cached resopnse and check if it exists
      NSURLCache.sharedURLCache().removeCachedResponseForRequest(request)
      response = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
      print(response)
      // And it does: bad!
      
      // Try removing all cached responses and check if it exists
      NSURLCache.sharedURLCache().removeAllCachedResponses()
      response = NSURLCache.sharedURLCache().cachedResponseForRequest(request)
      print(response)
      // And it doesn&#39;t: good! But odd...
    }
   
}
</code></pre>
<p><strong>那么<em>如何</em>删除单个请求的缓存响应呢?</strong>这是意外行为吗?还是 <code>NSURLCache</code> 行为正确而我只是遗漏了一些东西?提前感谢您查看!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我记得大多数 URL 缓存更改不是同步的。它们仅在您返回运行循环并允许发生各种异步回调后才会真正发生。</p>

<p>尝试在延迟 3-5 秒后异步运行其余代码,看看请求是否已被删除。</p>

<p>如果这不能解决问题,请提交错误。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - NSURLCache Alamofire 删除缓存的响应,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38729214/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38729214/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - NSURLCache Alamofire 删除缓存的响应