URLresponse is not retrieved after storing in cache using storeCachedResponse

前端 未结 3 1915
盖世英雄少女心
盖世英雄少女心 2020-12-03 23:40

Goal

I\'m trying to inject data/response from URLRequest into another URLRequest in my cache.

Setup

This is just a sample code. It\'s re

3条回答
  •  死守一世寂寞
    2020-12-04 00:00

    I resolved this problem by replacing first guard inside dataTask completionHandler with:

    guard error == nil else {
      print(error)
    
      if let cr = session.configuration.urlCache?.cachedResponse(for: urlRequest){
         let image = UIImage(data: cr.data)
         DispatchQueue.main.async {
           self?.imageView.image = image
         }
       }
    
       return
    }
    

    If request fails, it will take cached response for that request

提交回复
热议问题