Last active
January 31, 2021 22:06
-
-
Save jasdev/9748045de5e11ec14b200827bbf981cf to your computer and use it in GitHub Desktop.
Locked cancellable storage example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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