In a SwiftData @Model class, when do you mark a property @Transient instead of making it computed, and how does SwiftData treat it?
answer
- stored but never saved
- computed is already transient
- default value required
- fresh instances start from the default
basics
~20 sUse @Transient for stored state that must not be saved, such as a cached streak; computed properties are never saved anyway. It needs a default unless optional, stays out of the schema, and a freshly fetched instance starts at that default.
solid answer
~50 sSwiftData persists every stored property of a `@Model` class by default, and treats computed properties as transient automatically. `@Transient` is for the third case: state that must be **stored** on the object but never written to disk, such as a cached streak that is expensive to recompute or an `isSyncing` flag. The macro leaves it as a plain stored property, omits it from the schema, and requires a default value unless the type is optional; the compiler reports "@Transient requires non-optional property 'cachedStreak' to have a default value". The default matters because an instance SwiftData materializes from the store starts with it. Two consequences catch people out: the value is lost whenever a new instance is fetched, and the macro adds no Observation tracking for it, so changing it alone does not refresh a SwiftUI view.
code
swift · 18 linesimport Foundation
import SwiftData
@Model
final class Habit {
var name: String
var lastCheckIn: Date?
@Transient var cachedStreak: Int = 0
@Transient var isSyncing = false
var checkedInToday: Bool {
lastCheckIn.map { Calendar.current.isDateInToday($0) } ?? false
}
init(name: String) {
self.name = name
}
}go deeper
Remember that computed properties are never saved and that @Transient keeps a stored property out of the store.
Explain why @Transient needs a default value and why a freshly fetched instance starts from that default.
Catch the Observation gap: a view reading a transient value does not refresh when only that value changes, so prefer computed properties for UI.
Set a team rule for derived state on models: computed by default, @Transient caches only where profiling shows recomputation costs.
## Three kinds of property in a model A `@Model` class can hold three kinds of property, and SwiftData treats each differently: | Kind | Example | Persisted | Observed by SwiftUI | |---|---|---|---| | Stored | `var lastCheckIn: Date?` | Yes | Yes | | Computed | `var checkedInToday: Bool { ... }` | No | Through the stored values it reads | | `@Transient` stored | `@Transient var cachedStreak = 0` | No | No | Apple's documentation for the macro notes that SwiftData considers computed properties transient by default, so they never need the annotation. `@Transient` exists for **stored** state that should not be saved. ## When @Transient is the right tool Reach for it when a value must be held on the object itself rather than recomputed on every read: - A **cache** of something expensive to derive, such as a streak computed from many check-ins. - **In-flight flags** such as `isSyncing` or `isDownloading`, which mean nothing after a relaunch. - Runtime-only helpers that must not leak into the store's schema. If the value is cheap to derive from persisted properties, a computed property is simpler and always current. ## Why it needs a default value With the Swift 6.4 compiler, `@Transient var cachedStreak: Int` fails with "@Transient requires non-optional property 'cachedStreak' to have a default value", even when `init` assigns it. Apple's documentation gives the reason: the constraint lets SwiftData materialize instances of the class when running fetches. A fetched instance is built through the generated `init(backingData:)`, not through your initializer, so the only value a transient property can start with is its declared default, or `nil` for an optional. ## What SwiftData does with it Expanding the macro and running a small test store show the full behaviour: 1. The property gets **no generated accessors**: it stays a plain stored property on that object. 2. It is **absent from the schema**: `Schema([Habit.self])` lists `name` and `lastCheckIn` as attributes, not `cachedStreak`. 3. It lives **only in that instance's memory**. Fetching the same record through a second `ModelContext` produced a new instance whose `cachedStreak` was back to `0`. 4. It is **not tracked by Observation**: the macro emits no `access` or `withMutation` calls for it. In a test, `withObservationTracking` fired when a persisted property changed but stayed silent when `cachedStreak` changed. Point 4 matters most in SwiftUI. A view that reads `habit.cachedStreak` is not invalidated by a change to that property alone; it redraws only when something it observes changes. A computed property, by contrast, reads persisted properties through their observed accessors, so views that read it stay current. Because a transient property has no column in the store, keep fetch filters and sort orders on persisted properties. ## Choosing between computed and @Transient Work through these questions for any value a model needs that should not be saved: 1. **Can it be derived from persisted properties?** Then make it computed. It is always current, needs no default, and views that read it refresh when its inputs change. 2. **Is deriving it expensive enough to measure?** Walking hundreds of check-ins on every read of a streak in a scrolling list may be. Only then cache it in a `@Transient` property, and decide who recomputes it and when. 3. **Is it state about an operation rather than the data?** Flags such as `isSyncing` belong to the work in progress. A `@Transient` property works, but state that drives the UI often sits better in the view or in a separate `@Observable` object that SwiftUI tracks. 4. **Must it survive a relaunch?** Then it is not transient at all; persist it as an ordinary property. ## Wrong tools for the same job - `@ObservationIgnored` is Observation's marker, not SwiftData's. On a `@Model` stored property the 6.4 compiler rejects it with "expansion of macro 'ObservationIgnored()' produced an unexpected getter". - `private` changes visibility, not persistence: a private stored property still appears in the schema and is saved. - `@Attribute` options refine how a value is stored; none of them stops it being stored.
- Why doesn't a SwiftUI view refresh when a @Transient property of a SwiftData model changes?Because `@Model` generates Observation tracking only for the properties it persists. Their accessors call the registrar's `access` and `withMutation`; a `@Transient` property stays a plain stored property with no such calls, so changing it alone invalidates nothing. Derive the value with a computed property over persisted ones, or keep that UI state in the view or a separate `@Observable` object.
- Can you use @ObservationIgnored on a SwiftData @Model property to keep it out of the store?No. `@ObservationIgnored` belongs to the Observation framework, and on a `@Model` stored property the Swift 6.4 compiler rejects it with "expansion of macro 'ObservationIgnored()' produced an unexpected getter", because `@Model` already generates that property's accessors. `@Transient` is SwiftData's marker for a stored property it should not persist.
A sticky note on a printed form: it travels with that paper copy while you hold it, but the archive keeps only the form, so a copy pulled from the archive later arrives without the note.
- Computed properties need @Transient or SwiftData will try to save them.
- A @Transient property keeps its value after the app relaunches.
- @Transient works without a default value as long as init sets it.
- Changing a @Transient property redraws views like a persisted property.
- Marking a model property private keeps SwiftData from saving it.