Apple launches SwiftData, a core data replacement that supports SwiftUI

2023-06-13

At WWDC2023, Apple introduced SwiftData, a new framework that provides a declarative, Swift-first API for data persistence in iOS applications. SwiftData makes it easy to turn a Swift class into a persistent model, making it particularly suitable for use with SwiftUI. A few years ago, Apple completely changed the way UI is defined on its platform with SwiftUI. Now, Apple is taking the first step beyond Core Data, a persistence framework based on ORM that tightly binds to Objective-C's dynamic capabilities. While Core Data provides an API for defining persistent models, it typically requires the use of the model editor integrated in Xcode. The generated .xcdatamodeld file serves as the source of truth and is used to automatically generate stub code. These stub codes provide a base class for the actual model classes. SwiftData takes the opposite approach, where the code is the true source of the persistent data model. Here is an example of defining a simple model: ```swift @Model struct Recipe { @PersistedProperty var name: String @PersistedProperty var ingredients: [String] @PersistedProperty var instructions: String } ``` It is important to note that SwiftData relies on a new macro feature introduced in Swift 5.9. Behind the scenes, the `@Model` macro applies `@PersistedProperty` to each property in Recipe and adds initialization code and other required properties to the class implementation. Due to this additional logic being completely transparent to developers, SwiftData is able to map all properties to the underlying storage. According to Apple, persistent objects can be retrieved from the persistent storage and updated when needed. While simple types like Bool, Int, and String are used by default, more complex types must conform to the Codable protocol to be used in persistent classes. As mentioned earlier, SwiftData is particularly suitable for use with SwiftUI. In fact, the `@Model` macro also makes the class conform to the ObservableObject protocol and makes each persistent property behave like a `@Published` property. Additionally, to connect SwiftUI views to persistent models, you simply use the new `@Query` property, similar to using `@State` and `@Binding`. `@Query` ensures that the view is automatically re-rendered when the underlying data changes. SwiftData also supports defining predicates that can be used to filter data. Predicates are checked at compile time and raise compilation errors when types do not match. Additionally, SwiftData models can be synchronized between devices using CloudKit or stored as documents in iCloud Drive. To make it easier for developers to migrate to SwiftData, the framework supports incremental adoption in existing Core Data applications. Apple provides a sample project that demonstrates how to use SwiftData in only a portion of a Core Data application. Finally, it is important to note that SwiftData is not a wrapper for Core Data. As demonstrated by Apple at WWDC, SwiftData leverages the same persistence engine as Core Data, but it is a completely independent stack. Source: [https://www.infoq.com/news/2023/06/swiftdata-persistent-framework/](https://www.infoq.com/news/2023/06/swiftdata-persistent-framework/)

RECENT AI NEWS

RECENT AI TOOLS