This commit is contained in:
Sindre Sorhus 2024-12-12 14:11:14 +01:00
parent a6fc06aa1b
commit 3d98a76aab
3 changed files with 8 additions and 4 deletions

View File

@ -285,7 +285,7 @@ extension Defaults {
continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
observation.invalidate()
}
}
@ -334,7 +334,7 @@ extension Defaults {
continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
for observation in immutableObservations {
observation.invalidate()
}
@ -378,7 +378,7 @@ extension Defaults {
continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async {
Task { @MainActor in
for observation in observations {
observation.invalidate()
}

View File

@ -75,8 +75,9 @@ Access stored values from SwiftUI.
This is similar to `@AppStorage` but it accepts a ``Defaults/Key`` and many more types.
*/
@MainActor
@propertyWrapper
public struct Default<Value: Defaults.Serializable>: DynamicProperty {
public struct Default<Value: Defaults.Serializable>: @preconcurrency DynamicProperty {
@_documentation(visibility: private)
public typealias Publisher = AnyPublisher<Defaults.KeyChange<Value>, Never>
@ -254,8 +255,10 @@ extension Defaults.Toggle {
}
}
@MainActor
@propertyWrapper
private struct ViewStorage<Value>: DynamicProperty {
@MainActor
private final class ValueBox {
var value: Value

View File

@ -41,6 +41,7 @@ final class DefaultsSwiftUITests {
Defaults.removeAll(suite: suite_)
}
@MainActor
@Test
func testSwiftUIObserve() {
let view = ContentView()