8000 FIRApp: thread safety fixes (#2639) · firebase/firebase-ios-sdk@7b72589 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b72589

Browse files
maksymmalyhinCorrob
authored andcommitted
FIRApp: thread safety fixes (#2639)
1 parent 006dca9 commit 7b72589

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

Firebase/Core/FIRApp.m

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ + (void)configureWithName:(NSString *)name options:(FIROptions *)options {
166166
}
167167
}
168168

169-
if (sAllApps && sAllApps[name]) {
170-
[NSException raise:kFirebaseCoreErrorDomain
171-
format:@"App named %@ has already been configured.", name];
169+
@synchronized(self) {
170+
if (sAllApps && sAllApps[name]) {
171+
[NSException raise:kFirebaseCoreErrorDomain
172+
format:@"App named %@ has already been configured.", name];
173+
}
172174
}
173175

174176
FIRLogDebug(kFIRLoggerCore, @"I-COR000002", @"Configuring app named %@", name);
@@ -214,18 +216,19 @@ + (NSDictionary *)allApps {
214216
if (!sAllApps) {
215217
FIRLogError(kFIRLoggerCore, @"I-COR000005", @"No app has been configured yet.");
216218
}
217-
NSDictionary *dict = [NSDictionary dictionaryWithDictionary:sAllApps];
218-
return dict;
219+
return [sAllApps copy];
219220
}
220221
}
221222

222223
// Public only for tests
223224
+ (void)resetApps {
224-
sDefaultApp = nil;
225-
[sAllApps removeAllObjects];
226-
sAllApps = nil;
227-
[sLibraryVersions removeAllObjects];
228-
sLibraryVersions = nil;
225+
@synchronized(self) {
226+
sDefaultApp = nil;
227+
[sAllApps removeAllObjects];
228+
sAllApps = nil;
229+
[sLibraryVersions removeAllObjects];
230+
sLibraryVersions = nil;
231+
}
229232
}
230233

231234
- (void)deleteApp:(FIRAppVoidBoolCallback)completion {
@@ -423,8 +426,10 @@ + (void)sendNotificationsToSDKs:(FIRApp *)app {
423426

424427
// This is the new way of sending information to SDKs.
425428
// TODO: Do we want this on a background thread, maybe?
426-
for (Class<FIRLibrary> library in sRegisteredAsConfigurable) {
427-
[library configureWithApp:app];
429+
@synchronized(self) {
430+
for (Class<FIRLibrary> library in sRegisteredAsConfigurable) {
431+
[library configureWithApp:app];
432+
}
428433
}
429434
}
430435

@@ -476,10 +481,12 @@ + (void)registerLibrary:(nonnull NSString *)name withVersion:(nonnull NSString *
476481
// add the name/version pair to the dictionary.
477482
if ([name rangeOfCharacterFromSet:disallowedSet].location == NSNotFound &&
478483
[version rangeOfCharacterFromSet:disallowedSet].location == NSNotFound) {
479-
if (!sLibraryVersions) {
480-
sLibraryVersions = [[NSMutableDictionary alloc] init];
484+
@synchronized(self) {
485+
if (!sLibraryVersions) {
486+
sLibraryVersions = [[NSMutableDictionary alloc] init];
487+
}
488+
sLibraryVersions[name] = version;
481489
}
482-
sLibraryVersions[name] = version;
483490
} else {
484491
FIRLogError(kFIRLoggerCore, @"I-COR000027",
485492
@"The library name (%@) or version number (%@) contain invalid characters. "
@@ -508,20 +515,24 @@ + (void)registerInternalLibrary:(nonnull Class<FIRLibrary>)library
508515
dispatch_once(&onceToken, ^{
509516
sRegisteredAsConfigurable = [[NSMutableArray alloc] init];
510517
});
511-
[sRegisteredAsConfigurable addObject:library];
518+
@synchronized(self) {
519+
[sRegisteredAsConfigurable addObject:library];
520+
}
512521
}
513522
[self registerLibrary:name withVersion:version];
514523
}
515524

516525
+ (NSString *)firebaseUserAgent {
517-
NSMutableArray<NSString *> *libraries =
518-
[[NSMutableArray<NSString *> alloc] initWithCapacity:sLibraryVersions.count];
519-
for (NSString *libraryName in sLibraryVersions) {
520-
[libraries
521-
addObject:[NSString stringWithFormat:@"%@/%@", libraryName, sLibraryVersions[libraryName]]];
522-
}
523-
[libraries sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
524-
return [libraries componentsJoinedByString:@" "];
526+
@synchronized(self) {
527+
NSMutableArray<NSString *> *libraries =
528+
[[NSMutableArray<NSString *> alloc] initWithCapacity:sLibraryVersions.count];
529+
for (NSString *libraryName in sLibraryVersions) {
530+
[libraries addObject:[NSString stringWithFormat:@"%@/%@", libraryName,
531+
sLibraryVersions[libraryName]]];
532+
}
533+
[libraries sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
534+
return [libraries componentsJoinedByString:@" "];
535+
}
525536
}
526537

527538
- (void)checkExpectedBundleID {

0 commit comments

Comments
 (0)
0