Skip to content

Use an URLSessionConfiguration that can be configured by app #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ extension URLCache {

Remember when setting the cache the response (in this case our image) must be no larger than about 5% of the disk cache (See [this discussion](https://developer.apple.com/documentation/foundation/nsurlsessiondatadelegate/1411612-urlsession#discussion)).

You can also modify the `URLSessionConfiguration` used by `CachedAsyncImage` via `URLSessionConfiguration.cachedAsyncImage`:

```swift
URLSessionConfiguration.cachedAsyncImage.protocolClasses = [MyContentProtocol.self]
```

## Installation

1. In Xcode, open your project and navigate to **File** → **Swift Packages** → **Add Package Dependency...**
Expand Down
20 changes: 19 additions & 1 deletion Sources/CachedAsyncImage/CachedAsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public struct CachedAsyncImage<Content>: View where Content: View {
/// - content: A closure that takes the load phase as an input, and
/// returns the view to display for the specified phase.
public init(urlRequest: URLRequest?, urlCache: URLCache = .shared, scale: CGFloat = 1, transaction: Transaction = Transaction(), @ViewBuilder content: @escaping (AsyncImagePhase) -> Content) {
let configuration = URLSessionConfiguration.default
let configuration = URLSessionConfiguration.cachedAsyncImage
configuration.urlCache = urlCache
self.urlRequest = urlRequest
self.urlSession = URLSession(configuration: configuration)
Expand Down Expand Up @@ -404,3 +404,21 @@ private extension URLSession {
return (data, response, controller.metrics!)
}
}

// MARK: - URLSessionConfiguration

@MainActor
private var urlSessionConfig = URLSessionConfiguration.default
extension URLSessionConfiguration {

/// Default configuration used by `CachedAsyncImage`.
@MainActor
public static var cachedAsyncImage: URLSessionConfiguration {
get {
urlSessionConfig
}
set {
urlSessionConfig = newValue
}
}
}