How can I use local data in swift
Asked on 06/09/2025
1 search
To use local data in Swift, you can leverage the Swift Data framework, which provides a robust way to model and persist your app's data across launches. Here are some key points on how you can use local data with Swift Data:
-
Model Layer: Swift Data allows you to build your app's model layer using Swift classes. You can define your schema by applying the
@Model
macro to your classes. This is the foundation for persisting data locally. -
Custom Data Stores: You can create custom data stores to use different storage backends. For example, you can use SQLite, a remote web service, or even JSON files as your data store. This flexibility allows you to choose the best storage solution for your app's needs. You can learn more about this in the session Create a custom data store with SwiftData.
-
History Tracking: Swift Data provides a history API that allows you to track changes in your data store over time. This is useful for syncing local changes with a remote server or for understanding how data has changed. You can explore this feature in the session Track model changes with SwiftData history.
-
Querying and Indexing: You can optimize your data queries by using the
@Index
macro to specify which properties should be indexed. This makes queries more efficient, especially for properties that are frequently used in sorting and filtering. More details can be found in the session What’s new in SwiftData.
By using these features, you can effectively manage and utilize local data in your Swift applications.

Platforms State of the Union
Discover the newest advancements on Apple platforms.

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”.

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.