dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

Protocol cache policy. Please comment #124

Closed dmytro-anokhin closed 3 years ago

dmytro-anokhin commented 3 years ago

I'm working on a feature to opt in for using protocol cache in the package. Basically, instead of custom in-memory/disk cache, use NSURLCache and NSURLRequest.CachePolicy.

The main reason is that protocol cache supports Cache-Control header. Also you may use another library that caches images in shared NSURLCache.

As a trade-off you lose some control, like in-memory caching, download delays, expiry intervals (that you get with Cache-Control header). It also only properly works for in-memory downloads (using URLSessionDataTask).

You can follow development in branch protocol_cache_policy

Here is how to configure the package:

URLImageService.shared.defaultOptions.cachePolicy = .useProtocol

// Download using `URLSessionDataTask` 
URLImageService.shared.defaultOptions.loadOptions.formUnion(.inMemory)

// Set your `NSURLRequest.CachePolicy`
URLImageService.shared.defaultOptions.urlRequestConfiguration.cachePolicy = .returnCacheDataElseLoad

Please test it and let me know ideas/feedback you have. Cheers!

dmytro-anokhin commented 3 years ago

Merged