Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active Jan 31, 2021
Embed
What would you like to do?
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