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 continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice. // `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async { Task { @MainActor in
observation.invalidate() observation.invalidate()
} }
} }
@ -334,7 +334,7 @@ extension Defaults {
continuation.onTermination = { _ in continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice. // `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async { Task { @MainActor in
for observation in immutableObservations { for observation in immutableObservations {
observation.invalidate() observation.invalidate()
} }
@ -378,7 +378,7 @@ extension Defaults {
continuation.onTermination = { _ in continuation.onTermination = { _ in
// `invalidate()` should be thread-safe, but it is not in practice. // `invalidate()` should be thread-safe, but it is not in practice.
DispatchQueue.main.async { Task { @MainActor in
for observation in observations { for observation in observations {
observation.invalidate() 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. This is similar to `@AppStorage` but it accepts a ``Defaults/Key`` and many more types.
*/ */
@MainActor
@propertyWrapper @propertyWrapper
public struct Default<Value: Defaults.Serializable>: DynamicProperty { public struct Default<Value: Defaults.Serializable>: @preconcurrency DynamicProperty {
@_documentation(visibility: private) @_documentation(visibility: private)
public typealias Publisher = AnyPublisher<Defaults.KeyChange<Value>, Never> public typealias Publisher = AnyPublisher<Defaults.KeyChange<Value>, Never>
@ -254,8 +255,10 @@ extension Defaults.Toggle {
} }
} }
@MainActor
@propertyWrapper @propertyWrapper
private struct ViewStorage<Value>: DynamicProperty { private struct ViewStorage<Value>: DynamicProperty {
@MainActor
private final class ValueBox { private final class ValueBox {
var value: Value var value: Value

View File

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