Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Locked cancellable storage example.
final class SomeObservableObject: ObservableObject {
@Published var count: Int?
private let lock = NSRecursiveLock()
private var cancellables = Set<AnyCancellable>()
func fetchCount() {
lock.sync {
countPublisher
.sink(receiveValue: { [weak self] in self?.count = $0 })
.store(in: &cancellables)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment