what is the difference between @Transient and @Attribute(.ephemeral)

Generated on 8/9/2024

1 search

In the context of SwiftData, @Transient and @Attribute(.ephemeral) are used to manage how data is persisted in your models.

  • @Transient: This is used to mark a stored property in a model as transient, meaning that the data for this property will not be persisted. It is useful when you have data that is only needed temporarily and does not need to be saved to the database.

  • @Attribute(.ephemeral): This is a modifier used to mark attributes in a persistent model that should be preserved even when the model is deleted. These attributes are preserved as tombstone values, allowing you to process history information for deleted models. This is particularly useful when you need to retain certain information for historical tracking or auditing purposes.

Both of these features are part of the customization options available in SwiftData to manage how data is stored and maintained over time. For more details, you can refer to the session What’s new in SwiftData (02:43).

Track model changes with SwiftData history

Track model changes with SwiftData history

Reveal the history of your model’s changes with SwiftData! Use the history API to understand when data store changes occurred, and learn how to use this information to build features like remote server sync and out-of-process change handing in your app. We’ll also cover how you can build support for the history API into a custom data store.

What’s new in App Intents

What’s new in App Intents

Learn about improvements and all-new features with App Intents, and discover how this framework can help you expose your app’s functionality to Siri, Spotlight, Shortcuts, and more. We’ll show you how to make your entities more meaningful to the platform with the Transferable API, File Representations, new IntentFile APIs, and Spotlight Indexing, opening up powerful functionality in Siri and the Shortcuts app. Empower your intents to take people deep into your app with URL Representable Entities. Explore new techniques to model your entities and intents with new APIs for error handling and union values.

What’s new in SwiftData

What’s new in SwiftData

SwiftData makes it easy to add persistence to your app with its expressive, declarative API. Learn about refinements to SwiftData, including compound uniqueness constraints, faster queries with #Index, queries in Xcode previews, and rich predicate expressions. Join us to explore how you can use all of these features to express richer models and improve performance in your app. To discover how to build a custom data store or use the history API in SwiftData, watch “Create a custom data store with SwiftData” and “Track model changes with SwiftData history”.