SwiftedMind
(Dennis Müller)
1
Hey, I have a question about Sendable. I'm not sure if this is too SwiftUI-specific.
According to Apple's documentation, @State is thread safe. But it is not Sendable. I'm not sure I understand the difference between those two concepts. Shouldn't something that's thread safe also be Sendable? Or am I missing something?
The reason I'm asking is because I have a custom property wrapper that contains a @State, but with full Concurrency checking enabled, I get a warning every time I try to pass an instance of it into a Task:
@propertyWrapper
struct MyType: DynamicProperty {
@State var isActive: Bool? = nil
public var wrappedValue: Self {
self
}
}
struct MyContentView: View {
@MyType var testState
var body: some View {
Text("A")
}
func test() {
Task {
// Warning: Capture of 'self' with non-sendable type 'MyContentView' in a `@Sendable` closure
let tes1t = testState
}
}
}
Would it be safe to mark MyType as @unchecked Sendable? Or does this depend on internal implementation logic of @State, which only Apple has?
Thanks for any help! :)
Jon_Shier
(Jon Shier)
2
Property wrappers can't currently conform to Sendable properly, the compiler doesn't add the conformance to the synthesized internal properties, like _wrappedValue. I'm also not sure it would play well with DynamicProperty either. In general, you really don't want to perform async work from your SwiftUI views.
SwiftedMind
(Dennis Müller)
3
Ah okay, that makes sense.
Thank you :)