Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Config] Prefer NSLock over objc_sync_enter for synchronizing value types #14041

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions FirebaseRemoteConfig/SwiftNew/UserDefaultsManager.swift
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading