BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Apple Introduces SwiftData, a SwiftUI-Ready Core Data Replacement

Apple Introduces SwiftData, a SwiftUI-Ready Core Data Replacement

At WWDC 2023, Apple introduced SwiftData, a new framework that provides a declarative, Swift-first API enabling to work with data persistence in iOS apps. SwiftData can easily make a Swift class into a persistent model and is especially suited to be used with SwiftUI.

After radically changing the way UIs can be defined on its platforms a few years ago with SwiftUI, Apple is now moving its first steps to move beyond Core Data, an ORM-based persistency framework strictly tied to Objective-C dynamic capabilities.

While it provides an API that enabled the definition of persistent models, albeit in a rather cumbersome way, Core Data is usually used through the Schema Model Editor integrated in Xcode. The resulting .xcdatamodeld file is therefore the source of truth, which is then used to automatically generate stubs. The stubs provide base classes used as base classes for the actual model classes.

SwiftData takes the opposite approach, where the code is the source of truth about the persistent data model. This is how you can define a simple model:

@Model
class Recipe {
    @Attribute(.unique) var name: String
    var summary: String?
    var ingredients: [Ingredient]
}

Note how SwiftData relies on a new macro feature introduced in Swift 5.9. Under the hood, the @Model macro will apply the @PersistedProperty to each property in Recipe, as well as add initialization code and other required properties to the class implementation.

Thanks to this additional logic, completely transparent to the developer, SwiftData is able to map all properties to the underlying storage. According to Apple, persistent objects are fetched from and updated to the persistent storage when required.

While simple types, including Bool, Int, and String, are supported out-of-the-box, more complex types must conform to the Codable protocol in order to be used in a persistent class.

As mentioned, SwiftData is particularly suited for use with SwiftUI. Indeed, the @Model macro will also make a class conform to the ObservableObject protocol, as well as have each persisted property behave as a @Published property. In addition to this, to connect a SwiftUI View to the persistent model, you only need to use a new @Query attribute, similarly to how you use @State and @Binding. @Query will ensure the view will be automatically re-rendered each time the underlying data change.

SwiftData also provides support for the definition of predicates that can be used to filter data. Predicates are checked at compile-time and will raise compilation errors in case of type mismatch. Additionally, a SwiftData model can be synchronized across devices using CloudKit or as documents stored in iCloud Drive.

To make it easier for developers to migrate to SwiftData, the framework supports incremental adoption in an existing Core Data app. Apple is providing a sample project showing how you can use SwiftData only for a portion of a Core Data app.

As a final note, it is worth noting that SwiftData is not a wrapper around Core Data. As Apple showed at WWDC indeed, SwiftData leverages the same persistent engine that Core Data uses, but is a completely independent stack.

About the Author

Rate this Article

Adoption
Style

BT