Skip to content

Instantly share code, notes, and snippets.

Last active Jan 31, 2021
What would you like to do?
Sketch of a possible thread-safety issue.
import Combine
import Foundation
import SwiftUI
final class SomeObservableObject: ObservableObject {
@Published var count: Int?
private var cancellables = Set<AnyCancellable>()
func fetchCount() {
countPublisher // (Assume it’s defined elsewhere.)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] in self?.count = $0 })
.store(in: &cancellables) // Very likely triggers a `Set.insert(_:)` call
// under the hood.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment