Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 31, 2021 20:49
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/3390c43d90521009c04d17c96f346b05 to your computer and use it in GitHub Desktop.
Save jasdev/3390c43d90521009c04d17c96f346b05 to your computer and use it in GitHub Desktop.
Sketch of a possible AnyCancellable.store(in:) 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.
// https://github.com/OpenCombine/OpenCombine/blob/28993ae57de5a4ea7e164787636cafad442d568c/Sources/OpenCombine/AnyCancellable.swift#L48-L65
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment