Thread-Safe Singleton
Thread-safe singleton pattern using NSLock for mutable shared state.
final class AppConfiguration: @unchecked Sendable {
static let shared = AppConfiguration()
private let lock = NSLock()
private var _apiBaseURL: URL
private var _timeout: TimeInterval
var apiBaseURL: URL {
lock.withLock { _apiBaseURL }
}
var timeout: TimeInterval {
lock.withLock { _timeout }
}
private init() {
_apiBaseURL = URL(string: "https://api.example.com")!
_timeout = 30
}
func configure(apiBaseURL: URL, timeout: TimeInterval) {
lock.withLock {
_apiBaseURL = apiBaseURL
_timeout = timeout
}
}
}
// Usage:
// AppConfiguration.shared.configure(apiBaseURL: url, timeout: 60)
// let baseURL = AppConfiguration.shared.apiBaseURL