【问题标题】:NSURLCache Alamofire removing cached responsesNSURLCache Alamofire 删除缓存的响应
【发布时间】:2016-12-08 07:49:59
【问题描述】:

我正在使用以下代码来测试NSURLCache 中的行为。我在AppDelegate 中初始化了一个API 实例。我根据 Alamofire 的文档配置管理器,配置共享缓存,并分配 dataTaskWillCacheResponse 以确保响应确实会被缓存。

然后我调用makeRequest 检查是否存在缓存响应(它不应该在第一次启动时出现),然后我使用我的manager 使用相同的 URL 发出请求,以便请求在整个过程中是等效的测试。

我在dataTaskWillCacheResponse 的断点被命中,我继续,responseJSON 块被执行并且是Successful 所以我使用请求performTests

  1. 首先,我检查响应是否被缓存。 它是:好!
  2. 其次,(这就是问题所在)我删除了该请求的缓存响应,然后检查它是否存在。 确实如此:糟糕!
  3. 第三,我检查删除所有缓存的响应是否会删除该响应。 确实:好! 但奇怪的是,这行得通,而之前仅删除单个响应的尝试没有...

代码如下:

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() -> 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: "http://jsonplaceholder.typicode.com/posts")!)
        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't: good! But odd...
    }
    
}

那么如何删除单个请求的缓存响应呢?这是意外行为吗?还是NSURLCache 行为正确而我只是错过了什么?提前感谢您查看!

【问题讨论】:

  • 我也有同样的问题,你找到解决办法了吗?
  • @Louis,我没有使用 iOS 11 或 Swift 4 进行测试,但据我回忆,iOS 10 中的默认行为确实不正确,因此我不得不编写自定义缓存。我不再拥有用于企业应用程序的缓存代码。

标签: ios swift caching alamofire nsurlcache


【解决方案1】:

我记得大多数 URL 缓存更改不是同步的。它们只有在您返回运行循环并允许发生各种异步回调后才会真正发生。

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

如果这不能解决问题,请提交错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2014-12-03
    • 2023-03-13
    • 1970-01-01
    • 2014-08-14
    • 2014-09-27
    相关资源
    最近更新 更多