22
22
23
23
NS_ASSUME_NONNULL_BEGIN
24
24
25
+ /* * A NSMutableDictionary of FirebaseApp name and bucket names to FIRStorage instance. */
26
+ typedef NSMutableDictionary <NSString *, FIRStorage *> FIRStorageDictionary;
27
+
25
28
@interface FIRStorage ()
26
29
// Surface the internal initializer to create instances of FIRStorage.
27
30
- (instancetype )initWithApp : (FIRApp *)app
@@ -31,6 +34,7 @@ - (instancetype)initWithApp:(FIRApp *)app
31
34
@end
32
35
33
36
@interface FIRStorageComponent () <FIRLibrary>
37
+ @property (nonatomic ) FIRStorageDictionary *instances;
34
38
// / Internal initializer.
35
39
- (instancetype )initWithApp : (FIRApp *)app ;
36
40
@end
@@ -43,6 +47,7 @@ - (instancetype)initWithApp:(FIRApp *)app {
43
47
self = [super init ];
44
48
if (self) {
45
49
_app = app;
50
+ _instances = [NSMutableDictionary dictionary ];
46
51
}
47
52
return self;
48
53
}
@@ -60,6 +65,7 @@ + (void)load {
60
65
isRequired: NO ];
61
66
FIRComponentCreationBlock creationBlock =
62
67
^id _Nullable (FIRComponentContainer *container, BOOL *isCacheable) {
68
+ *isCacheable = YES ;
63
69
return [[FIRStorageComponent alloc ] initWithApp: container.app];
64
70
};
65
71
FIRComponent *storageProvider =
@@ -74,10 +80,21 @@ + (void)load {
74
80
#pragma mark - FIRStorageInstanceProvider Conformance
75
81
76
82
- (FIRStorage *)storageForBucket : (NSString *)bucket {
77
- // Create an instance of FIRStorage and return it.
78
- id <FIRAuthInterop> auth = FIR_COMPONENT (FIRAuthInterop, self.app .container );
79
- id <FIRAppCheckInterop> appCheck = FIR_COMPONENT (FIRAppCheckInterop, self.app .container );
80
- return [[FIRStorage alloc ] initWithApp: self .app bucket: bucket auth: auth appCheck: appCheck];
83
+ FIRStorageDictionary *instances = [self instances ];
84
+ @synchronized (instances) {
85
+ FIRStorage *instance = instances[bucket];
86
+ if (!instance) {
87
+ // Create an instance of FIRStorage and return it.
88
+ id <FIRAuthInterop> auth = FIR_COMPONENT (FIRAuthInterop, self.app .container );
89
+ id <FIRAppCheckInterop> appCheck = FIR_COMPONENT (FIRAppCheckInterop, self.app .container );
90
+ instance = [[FIRStorage alloc ] initWithApp: self .app
91
+ bucket: bucket
92
+ auth: auth
93
+ appCheck: appCheck];
94
+ instances[bucket] = instance;
95
+ }
96
+ return instance;
97
+ }
81
98
}
82
99
83
100
@end
0 commit comments