@@ -73,15 +73,7 @@ import FirebaseCore
73
73
}
74
74
75
75
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)
85
77
}
86
78
87
79
/// The `FirebaseApp` associated with this Storage instance.
@@ -249,6 +241,31 @@ import FirebaseCore
249
241
250
242
// MARK: - Internal and Private APIs
251
243
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
+
252
269
let fetcherService = StorageFetcherService ( )
253
270
254
271
let dispatchQueue : DispatchQueue
@@ -281,13 +298,6 @@ import FirebaseCore
281
298
/// Once `configured` is true, the emulator can no longer be enabled.
282
299
var configured = false
283
300
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
-
291
301
var host : String
292
302
var scheme : String
293
303
var port : Int
0 commit comments