Contents

Transient()

Tells SwiftData not to persist the annotated property when managing the owning class.

Declaration

@attached(peer) macro Transient()

Mentioned in

Overview

If your model class has one or more stored properties that you want to omit from writes to the persistent storage, annotate each of those properties with the @Transient macro.

@Model
class RemoteImage {
    var sourceURL: URL
    var data: Data
    
    @Transient
    var isDownloading = false
    
    init(sourceURL: URL, data: Data = Data(), isDownloading: Bool) {
        self.sourceURL = sourceURL
        self.data = data
        self.isDownloading = isDownloading
    }
}

Unless the type of the annotated property is an optional, the @Transient macro requires you to provide a default value. This constraint enables SwiftData to successfully materialize instances of the enclosing model class when running fetches.

See Also

Model definition