Skip to content

Commit a3254ff

Browse files
authored
[Storage] Addressing Swift 6 issues with Storage's instance management (#13445)
1 parent 279ac2a commit a3254ff

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

FirebaseStorage/Sources/Storage.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,7 @@ import FirebaseCore
7373
}
7474

7575
private class func storage(app: FirebaseApp, bucket: String) -> Storage {
76-
os_unfair_lock_lock(&instancesLock)
77-
defer { os_unfair_lock_unlock(&instancesLock) }
78-
79-
if let instance = instances[bucket] {
80-
return instance
81-
}
82-
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
83-
instances[bucket] = newInstance
84-
return newInstance
76+
return InstanceCache.shared.storage(app: app, bucket: bucket)
8577
}
8678

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

250242
// MARK: - Internal and Private APIs
251243

244+
private final class InstanceCache: @unchecked Sendable {
245+
static let shared = InstanceCache()
246+
247+
/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
248+
/// instances of Storage associated with the given app.
249+
private var instances: [String: Storage] = [:]
250+
251+
/// Lock to manage access to the instances array to avoid race conditions.
252+
private var instancesLock: os_unfair_lock = .init()
253+
254+
private init() {}
255+
256+
func storage(app: FirebaseApp, bucket: String) -> Storage {
257+
os_unfair_lock_lock(&instancesLock)
258+
defer { os_unfair_lock_unlock(&instancesLock) }
259+
260+
if let instance = instances[bucket] {
261+
return instance
262+
}
263+
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
264+
instances[bucket] = newInstance
265+
return newInstance
266+
}
267+
}
268+
252269
let fetcherService = StorageFetcherService()
253270

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

284-
/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
285-
/// instances of Storage associated with the given app.
286-
private static var instances: [String: Storage] = [:]
287-
288-
/// Lock to manage access to the instances array to avoid race conditions.
289-
private static var instancesLock: os_unfair_lock = .init()
290-
291301
var host: String
292302
var scheme: String
293303
var port: Int

0 commit comments

Comments
 (0)