Hi, I usually have a singleton that contains a Combine's publisher like this:
import Combine
struct TestSingleton: Sendable {
static let shared = TestSingleton()
private init() {}
let publisher = PassthroughSubject<Void, Never>()
}
Swift 6 complete concurrency check will give me a compiler warning: Stored property 'publisher' of 'Sendable'-conforming struct 'TestSingleton' has non-sendable type 'PassthroughSubject<Void, Never>'
It seems like the PassthroughSubject is non-sendable. How should I deal with this? The compiler suggests adding @preconcurrency
to the Combine the import module line.
@preconcurrency import Combine
That silences the compiler error.
Question: Should I add that @preconcurrency
label? Another way is to add @MainActor
to the struct, but I'm unsure if I want this to be on the main thread. For context, the publisher here will emit a signal when the user clicks something on the UI.