0

My data is retrieve from a JSON API, and the data would changed per day. Currently, i'm using reloadIgnoringLocalCacheData now, which ignores the cache data and always retrieve data from the originating source.

My question is that could I just compare the cache with originating first and if they are not equal, then retrieve the data from server, otherwise use cache data. or maybe set an expired time for cache, how to implement that?

private var session: URLSession = {
        let config = URLSessionConfiguration.default
        config.urlCache = URLCache.shared
        config.waitsForConnectivity = true
        config.requestCachePolicy = .reloadIgnoringLocalCacheData
        return URLSession(configuration: config, delegate: nil, delegateQueue: nil)
    }()
    public enum CachePolicy : UInt {


        case useProtocolCachePolicy


        case reloadIgnoringLocalCacheData

        case reloadIgnoringLocalAndRemoteCacheData

        public static var reloadIgnoringCacheData: NSURLRequest.CachePolicy { get }


        case returnCacheDataElseLoad

        case returnCacheDataDontLoad


        case reloadRevalidatingCacheData
    }
ChuckZHB
  • 656
  • 2
  • 15
  • The JSON API should set HTTP expiry headers. Check the returned response with CURL or Charles etc. If the expiry header is not set, then you can implement your own expiry by just tracking and checking the last retrieved time. Assume, according to your stated rule, if the local data was retrieved yesterday, it needs to be re-fetched today – Dale Mar 09 '20 at 04:42
  • @Dale, thanks for reply, I checked with Charles and API website, and there is no expiry header in the response. So I could define my own expiry rule now right? But which Cache Policy I should use in that case? – ChuckZHB Mar 09 '20 at 07:01

0 Answers0