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

fix(ios): make callback map thread-safe to fix high write and notify rates #630 #642

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 }
}
}
}
Loading