Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 31, 2021 22:06
Show Gist options
  • Save jasdev/9748045de5e11ec14b200827bbf981cf to your computer and use it in GitHub Desktop.
Save jasdev/9748045de5e11ec14b200827bbf981cf to your computer and use it in GitHub Desktop.
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