Skip to content

Commit

Permalink
fix(ios): make callback map thread-safe to fix high write and notify …
Browse files Browse the repository at this point in the history
…rates #630 (#642)
  • Loading branch information
pwespi committed Apr 6, 2024
1 parent 0f01960 commit da9b782
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ios/Plugin.xcodeproj/project.pbxproj
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
03FC29A292ACC40490383A1F /* Pods_Plugin.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B2A61DA5A1F2DD4F959604D /* Pods_Plugin.framework */; };
20C0B05DCFC8E3958A738AF2 /* Pods_PluginTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F6753A823D3815DB436415E3 /* Pods_PluginTests.framework */; };
340BC4032BC1A447004AFEFC /* ThreadSafeDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 340BC4022BC1A447004AFEFC /* ThreadSafeDictionary.swift */; };
34E541D62962E2EA007544B1 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34E541D52962E2EA007544B1 /* Logging.swift */; };
34ECC0E325BE199F00881175 /* Device.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34ECC0E025BE199F00881175 /* Device.swift */; };
34ECC0E425BE199F00881175 /* Conversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34ECC0E125BE199F00881175 /* Conversion.swift */; };
Expand All @@ -33,6 +34,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
340BC4022BC1A447004AFEFC /* ThreadSafeDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThreadSafeDictionary.swift; sourceTree = "<group>"; };
34E541D52962E2EA007544B1 /* Logging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
34ECC0E025BE199F00881175 /* Device.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Device.swift; sourceTree = "<group>"; };
34ECC0E125BE199F00881175 /* Conversion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Conversion.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +102,7 @@
50ADFF8A201F53D600D50D53 /* Plugin */ = {
isa = PBXGroup;
children = (
340BC4022BC1A447004AFEFC /* ThreadSafeDictionary.swift */,
34E541D52962E2EA007544B1 /* Logging.swift */,
34ECC0E125BE199F00881175 /* Conversion.swift */,
34ECC0E025BE199F00881175 /* Device.swift */,
Expand Down Expand Up @@ -317,6 +320,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
340BC4032BC1A447004AFEFC /* ThreadSafeDictionary.swift in Sources */,
34ECC0E325BE199F00881175 /* Device.swift in Sources */,
34E541D62962E2EA007544B1 /* Logging.swift in Sources */,
34ECC0E425BE199F00881175 /* Conversion.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ios/Plugin/Device.swift
Expand Up @@ -6,7 +6,7 @@ class Device: NSObject, CBPeripheralDelegate {
typealias Callback = (_ success: Bool, _ value: String) -> Void

private var peripheral: CBPeripheral!
private var callbackMap = [String: Callback]()
private var callbackMap = ThreadSafeDictionary<String,Callback>()
private var timeoutMap = [String: DispatchWorkItem]()
private var servicesCount = 0
private var servicesDiscovered = 0
Expand Down
13 changes: 13 additions & 0 deletions ios/Plugin/ThreadSafeDictionary.swift
@@ -0,0 +1,13 @@
class ThreadSafeDictionary<K: Hashable, T> {
private var dictionary: [K: T] = [:]
private let queue = DispatchQueue(label: "threadSafeDictionaryQueue", attributes: .concurrent)

subscript(key: K) -> T? {
get {
return queue.sync { dictionary[key] }
}
set {
queue.async(flags: .barrier) { self.dictionary[key] = newValue }
}
}
}

0 comments on commit da9b782

Please sign in to comment.