Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 31, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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