Skip to content

Commit

Permalink
[Storage] Addressing Swift 6 issues with Storage's instance managem…
Browse files Browse the repository at this point in the history
…ent (#13445)
  • Loading branch information
ncooke3 authored Aug 2, 2024
1 parent 279ac2a commit a3254ff
Showing 1 changed file with 26 additions and 16 deletions.
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

0 comments on commit a3254ff

Please sign in to comment.