Skip to content

Commit

Permalink
[Config] Prefer NSLock over objc_sync_enter for synchronizing value t…
Browse files Browse the repository at this point in the history
…ypes (#14041)
  • Loading branch information
ncooke3 authored and paulb777 committed Nov 14, 2024
1 parent b1f46de commit b4e5bcf
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions FirebaseRemoteConfig/SwiftNew/UserDefaultsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class UserDefaultsManager: NSObject {
}
}

private static let sharedInstanceMapLock = NSLock()
private static var sharedInstanceMap: [String: UserDefaults] = [:]

/// Returns the shared user defaults instance for the given bundle identifier.
Expand All @@ -81,14 +82,14 @@ public class UserDefaultsManager: NSObject {
/// - Returns: The shared user defaults instance.
@objc(sharedUserDefaultsForBundleIdentifier:)
static func sharedUserDefaultsForBundleIdentifier(_ bundleIdentifier: String) -> UserDefaults {
objc_sync_enter(sharedInstanceMap)
defer { objc_sync_exit(sharedInstanceMap) }
if let instance = sharedInstanceMap[bundleIdentifier] {
return instance
sharedInstanceMapLock.withLock {
if let instance = sharedInstanceMap[bundleIdentifier] {
return instance
}
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName(for: bundleIdentifier))!
sharedInstanceMap[bundleIdentifier] = userDefaults
return userDefaults
}
let userDefaults = UserDefaults(suiteName: userDefaultsSuiteName(for: bundleIdentifier))!
sharedInstanceMap[bundleIdentifier] = userDefaults
return userDefaults
}

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

0 comments on commit b4e5bcf

Please sign in to comment.