Skip to content

Commit b07210a

Browse files
committed
Refactor PluralsLocalizationDataSource to use DispatchQueue for concurrent access; improve responsiveness during updates
1 parent e2ebe0e commit b07210a

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Sources/CrowdinSDK/CrowdinSDK/Localization/Provider/LocalizationDataSource.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,15 @@ class PluralsLocalizationDataSource: LocalizationDataSourceProtocol {
102102
case other
103103
}
104104

105-
// Use a lightweight lock to avoid blocking the main thread during reads in high-contention scenarios
106-
private let lock = NSLock()
105+
private let accessQueue = DispatchQueue(label: "com.crowdin.PluralsLocalizationDataSource.accessQueue", attributes: .concurrent)
107106
private var _plurals: [AnyHashable: Any]
108107
var plurals: [AnyHashable: Any] {
109-
// Attempt non-blocking read to avoid deadlocking UI during concurrent updates
110-
if lock.try() {
111-
defer { lock.unlock() }
112-
return _plurals
113-
} else {
114-
// If write is in progress, return empty snapshot to keep UI responsive
115-
return [:]
108+
get {
109+
var plurals: [AnyHashable: Any] = [:]
110+
accessQueue.sync {
111+
plurals = self._plurals
112+
}
113+
return plurals
116114
}
117115
}
118116

@@ -121,9 +119,9 @@ class PluralsLocalizationDataSource: LocalizationDataSourceProtocol {
121119
}
122120

123121
func update(with values: [AnyHashable: Any]) {
124-
lock.lock()
125-
_plurals = values
126-
lock.unlock()
122+
accessQueue.async(flags: .barrier) {
123+
self._plurals = values
124+
}
127125
}
128126

129127
func findKey(for string: String) -> String? {

0 commit comments

Comments
 (0)