Skip to content

Commit 601a687

Browse files
author
Daniel Browne
committed
Thread safe access to 'eventHandlers' dictionary
1 parent d405b36 commit 601a687

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/Models/PusherChannel.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ public enum PusherChannelType {
2626

2727
@objcMembers
2828
open class PusherChannel: NSObject {
29-
open var eventHandlers: [String: [EventHandler]] = [:]
29+
// Access via queue for thread safety if user binds/unbinds events to a channel off the main queue
30+
// (Concurrent reads are allowed. Writes using `.barrier` so queue waits for completion before continuing)
31+
private let eventHandlersQueue = DispatchQueue(label: "com.pusher.pusherswift-channel-event-handlers-\(UUID().uuidString)",
32+
attributes: .concurrent)
33+
private var eventHandlersInternal = [String: [EventHandler]]()
34+
open var eventHandlers: [String: [EventHandler]] {
35+
get {
36+
return eventHandlersQueue.sync { eventHandlersInternal }
37+
}
38+
set {
39+
eventHandlersQueue.async(flags: .barrier) { self.eventHandlersInternal = newValue }
40+
}
41+
}
3042
open var subscribed = false
3143
public let name: String
3244
open weak var connection: PusherConnection?

0 commit comments

Comments
 (0)