You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/ObservationGuide.md
+37-8Lines changed: 37 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,8 +15,10 @@ invalidation.
15
15
16
16
## Availability
17
17
18
-
MainActor Observation requires Swift Observation platform support:
18
+
MainActor Observation requires Swift compiler support for CDE's Observation runtime plus Swift
19
+
Observation platform support:
19
20
21
+
- Swift compiler 6.2+
20
22
- iOS 17+
21
23
- macOS 14+
22
24
- tvOS 17+
@@ -93,7 +95,7 @@ saved change for `title`, Swift Observation invalidates that reader.
93
95
Observation consumption is MainActor-bound and centered on `container.viewContext`.
94
96
95
97
- Read observed model objects from MainActor UI code.
96
-
-Keep`CDEObservationDomain` on MainActor.
98
+
-Create and explicitly invalidate`CDEObservationDomain` on MainActor.
97
99
- Treat background contexts as metadata producers only. They should not publish Observation changes
98
100
directly.
99
101
- If an opt-in model is used without a retained domain for its `viewContext`, reads can still compile,
@@ -126,7 +128,8 @@ Use the strongest producer route that matches the context you own.
126
128
| Source | Public API | Precision | Notes |
127
129
|---|---|---|---|
128
130
|`viewContext` save |`try viewContext.save()`| property-level | A retained domain instruments its own `viewContext`; `NSMainModelActor.saveObservedChanges(in:)` is symmetry sugar. |
129
-
|`@NSModelActor` background save |`try await saveObservedChanges(in: observation)`| property-level | Stages changed keys before save without suspending between staging and commit. |
131
+
| Observation-aware `@NSModelActor` background save | create the actor with `init(observationDomain:)`, then call `try modelContext.save()` or `try await saveObservedChanges()`| property-level after successful save | The generated initializer retains the domain and registration for its actor context. |
132
+
| Plain `@NSModelActor` background save |`try await saveObservedChanges(in: observation)`| property-level | Stages changed keys before save without suspending between staging and commit. |
130
133
| Arbitrary context wrapper |`try await observation.saveObservedChanges(in: context)`| property-level | Preferred when thrown-save cleanup matters; the wrapper rolls back its staged token and the context on failure. |
131
134
| Registered ordinary context |`observation.registerChangeProducer(context:)`, then plain `context.save()`| property-level after successful save | If a direct save throws, call `rollback()`, `reset()`, or invalidate the registration to clear staged notification state. |
132
135
| Convenience background context |`observation.newObservedBackgroundContext()`| property-level after successful save | Equivalent to `container.newBackgroundContext()` plus producer registration. |
@@ -136,7 +139,31 @@ Use the strongest producer route that matches the context you own.
136
139
137
140
## Background Actor Save
138
141
139
-
Prefer `NSModelActor.saveObservedChanges(in:)` for actor-owned background writes:
142
+
When the actor can be created on MainActor from the retained observation domain, prefer the generated
0 commit comments