Skip to content

Commit

Permalink
Merge pull request #1117 from anyproto/ios-2584-fix-passthroughsubjec…
Browse files Browse the repository at this point in the history
…tdictionary-crash-on-the-start

iOS-2584 Fix PassthroughSubjectDictionary crash on the start
  • Loading branch information
joe-pusya authored Apr 3, 2024
2 parents 4c16125 + 16d4d9d commit 8855d15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Combine

public final class PassthroughSubjectDictionary<K, V> where K: Hashable, V: Equatable {

private var dictionary = SynchronizedDictionary<K, AnytypePassthroughSubject<V?>>()
private let dictionary = SynchronizedDictionary<K, AnytypePassthroughSubject<V?>>()

public init() {}

Expand All @@ -29,7 +29,7 @@ public final class PassthroughSubjectDictionary<K, V> where K: Hashable, V: Equa
}

public func publishAllValues() {
dictionary.dictionary.values.forEach { $0.sendUpdate() }
dictionary.values.forEach { $0.sendUpdate() }
}

public func publishValue(for key: K) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import Foundation

public final class SynchronizedDictionary<K, V> where K: Hashable {

var dictionary: [K: V] = [:]

// MARK: - Private variables


private var dictionary: [K: V] = [:]
private let lock = NSLock()

// MARK: - Initializers
Expand Down Expand Up @@ -46,6 +45,14 @@ public final class SynchronizedDictionary<K, V> where K: Hashable {
return keys
}

public var values: Dictionary<K, V>.Values {
lock.lock()
let values = dictionary.values
lock.unlock()

return values
}

public func removeAll() {
lock.lock()
dictionary.removeAll()
Expand Down

0 comments on commit 8855d15

Please sign in to comment.