Created
April 24, 2022 11:41
-
-
Save Czajnikowski/643e37f3c9547604ea2291fd2eb0dba0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Combine | |
extension URLSession { | |
func cacheThenDataTaskPublisher( | |
for urlRequest: URLRequest | |
) -> some Publisher { | |
let cacheDataTaskPublisher = configuration.urlCache? | |
.cachedResponse(for: urlRequest) | |
.map { | |
Just((data: $0.data, response: $0.response)).eraseToAnyPublisher() | |
} ?? Empty().eraseToAnyPublisher() | |
return Publishers.Merge( | |
cacheDataTaskPublisher.mapError { switch $0 {} }, | |
dataTaskPublisher(for: urlRequest) | |
) | |
.removeDuplicates(by: { $0.data == $1.data }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment