Skip to content
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

[Storage] Addressing Swift 6 issues with Storage's instance management #13445

Merged
merged 6 commits into from
Aug 2, 2024
Merged
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
42 changes: 26 additions & 16 deletions FirebaseStorage/Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ import FirebaseCore
}

private class func storage(app: FirebaseApp, bucket: String) -> Storage {
os_unfair_lock_lock(&instancesLock)
defer { os_unfair_lock_unlock(&instancesLock) }

if let instance = instances[bucket] {
return instance
}
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
instances[bucket] = newInstance
return newInstance
return InstanceCache.shared.storage(app: app, bucket: bucket)
}

/// The `FirebaseApp` associated with this Storage instance.
Expand Down Expand Up @@ -249,6 +241,31 @@ import FirebaseCore

// MARK: - Internal and Private APIs

private final class InstanceCache: @unchecked Sendable {
static let shared = InstanceCache()

/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
/// instances of Storage associated with the given app.
private var instances: [String: Storage] = [:]

/// Lock to manage access to the instances array to avoid race conditions.
private var instancesLock: os_unfair_lock = .init()

private init() {}

func storage(app: FirebaseApp, bucket: String) -> Storage {
os_unfair_lock_lock(&instancesLock)
defer { os_unfair_lock_unlock(&instancesLock) }

if let instance = instances[bucket] {
return instance
}
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
instances[bucket] = newInstance
return newInstance
}
}

let fetcherService = StorageFetcherService()

let dispatchQueue: DispatchQueue
Expand Down Expand Up @@ -281,13 +298,6 @@ import FirebaseCore
/// Once `configured` is true, the emulator can no longer be enabled.
var configured = false

/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
/// instances of Storage associated with the given app.
private static var instances: [String: Storage] = [:]

/// Lock to manage access to the instances array to avoid race conditions.
private static var instancesLock: os_unfair_lock = .init()

var host: String
var scheme: String
var port: Int
Expand Down
Loading