Skip to content

Commit 28c0190

Browse files
author
Daniel Browne
committed
Thread safe access to 'channels' dictionary
1 parent 601a687 commit 28c0190

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/Models/PusherChannels.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ import Foundation
22

33
@objcMembers
44
@objc open class PusherChannels: NSObject {
5-
open var channels = [String: PusherChannel]()
5+
// Access via queue for thread safety if user subscribes/unsubscribes to a channel off the main queue
6+
// (Concurrent reads are allowed. Writes using `.barrier` so queue waits for completion before continuing)
7+
private let channelsQueue = DispatchQueue(label: "com.pusher.pusherswift-channels-\(UUID().uuidString)",
8+
attributes: .concurrent)
9+
private var channelsInternal = [String: PusherChannel]()
10+
open var channels: [String: PusherChannel] {
11+
get {
12+
return channelsQueue.sync { channelsInternal }
13+
}
14+
set {
15+
channelsQueue.async(flags: .barrier) { self.channelsInternal = newValue }
16+
}
17+
}
618

719
/**
820
Create a new PusherChannel, which is returned, and add it to the PusherChannels list

0 commit comments

Comments
 (0)