Skip to content

Commit d935c7f

Browse files
ncooke3paulb777
authored andcommitted
[Config] Prefer NSLock over objc_sync_enter for synchronizing value types (#14041)
1 parent 17ba35e commit d935c7f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

FirebaseRemoteConfig/SwiftNew/UserDefaultsManager.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class UserDefaultsManager: NSObject {
7373
}
7474
}
7575

76+
private static let sharedInstanceMapLock = NSLock()
7677
private static var sharedInstanceMap: [String: UserDefaults] = [:]
7778

7879
/// Returns the shared user defaults instance for the given bundle identifier.
@@ -81,14 +82,14 @@ public class UserDefaultsManager: NSObject {
8182
/// - Returns: The shared user defaults instance.
8283
@objc(sharedUserDefaultsForBundleIdentifier:)
8384
static func sharedUserDefaultsForBundleIdentifier(_ bundleIdentifier: String) -> UserDefaults {
84-
objc_sync_enter(sharedInstanceMap)
85-
defer { objc_sync_exit(sharedInstanceMap) }
86-
if let instance = sharedInstanceMap[bundleIdentifier] {
87-
return instance
85+
sharedInstanceMapLock.withLock {
86+
if let instance = sharedInstanceMap[bundleIdentifier] {
87+
return instance
88+
}
89+
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName(for: bundleIdentifier))!
90+
sharedInstanceMap[bundleIdentifier] = userDefaults
91+
return userDefaults
8892
}
89-
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName(for: bundleIdentifier))!
90-
sharedInstanceMap[bundleIdentifier] = userDefaults
91-
return userDefaults
9293
}
9394

9495
/// Returns the user defaults suite name for the given bundle identifier.

0 commit comments

Comments
 (0)