Skip to content

Commit

Permalink
Make instanceForProtocol:inContainer: return nullable T (#12391)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Feb 21, 2024
1 parent 3de7e6c commit 1ed6d66
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion FirebaseCore/Extension/FIRComponentType.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ NS_SWIFT_NAME(ComponentType)

/// Do not use directly. A factory method to retrieve an instance that provides a specific
/// functionality.
+ (T)instanceForProtocol:(Protocol *)protocol inContainer:(FIRComponentContainer *)container;
+ (nullable T)instanceForProtocol:(Protocol *)protocol
inContainer:(FIRComponentContainer *)container;

@end

Expand Down
3 changes: 2 additions & 1 deletion FirebaseCore/Sources/FIRComponentType.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

@implementation FIRComponentType

+ (id)instanceForProtocol:(Protocol *)protocol inContainer:(FIRComponentContainer *)container {
+ (nullable id)instanceForProtocol:(Protocol *)protocol
inContainer:(FIRComponentContainer *)container {
// Forward the call to the container.
return [container instanceForProtocol:protocol];
}
Expand Down
12 changes: 8 additions & 4 deletions FirebaseStorage/Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ import FirebaseCore
/// - Parameter app: The custom `FirebaseApp` used for initialization.
/// - Returns: A `Storage` instance, configured with the custom `FirebaseApp`.
@objc(storageForApp:) open class func storage(app: FirebaseApp) -> Storage {
let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
in: app.container)
guard let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
in: app.container) else {
fatalError("No \(StorageProvider.self) instance found for Firebase app: \(app.name)")
}
return provider.storage(for: Storage.bucket(for: app))
}

Expand All @@ -75,8 +77,10 @@ import FirebaseCore
/// URL.
@objc(storageForApp:URL:)
open class func storage(app: FirebaseApp, url: String) -> Storage {
let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
in: app.container)
guard let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
in: app.container) else {
fatalError("No \(StorageProvider.self) instance found for Firebase app: \(app.name)")
}
return provider.storage(for: Storage.bucket(for: app, urlString: url))
}

Expand Down
6 changes: 3 additions & 3 deletions FirebaseStorage/Tests/Unit/StorageComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class StorageComponentTests: StorageTestHelpers {
in: container)
XCTAssertNotNil(provider)

let storage1 = provider.storage(for: "randomBucket")
let storage2 = provider.storage(for: "randomBucket")
let storage1 = provider?.storage(for: "randomBucket")
let storage2 = provider?.storage(for: "randomBucket")
XCTAssertNotNil(storage1)

// Ensure they're the same instance.
XCTAssert(storage1 === storage2)

let storage3 = provider.storage(for: "differentBucket")
let storage3 = provider?.storage(for: "differentBucket")
XCTAssertNotNil(storage3)

XCTAssert(storage1 !== storage3)
Expand Down

0 comments on commit 1ed6d66

Please sign in to comment.