Apple Introduces SwiftData
Written by Kay Ewbank   
Thursday, 15 June 2023

Apple has introduced the SwiftData framework as a way of making it easier to persist data using declarative code. The framework is integrated with SwiftUI and can be used to query and filter data using regular Swift code.

The framework was announced in a preview version at Apple's WWDC23 conference. Apple says the framework combines Core Data’s proven persistence technology and Swift’s modern concurrency features, and enables developers to add persistence to apps quickly, with minimal code and no external dependencies.

swiftlogo

The data is modeled using regular Swift types with @Model, with no additional files or tools to manage. SwiftData can automatically infer many relationships and you can use clear declarations like @Attribute(.unique) to describe constraints.

swiftdata

By design, SwiftData supplements your existing model classes. The framework provides tools such as macros and property wrappers that can be used to describe your app’s schema in Swift code, removing any reliance on external dependencies such as model and migration mapping files.

Developers can describe the entire model layer (or object graph) for an app, and the framework handles storing the underlying model data, and optionally, syncing that data across multiple devices.

The SwiftData documentation says that the ability to preserve model data across launches is one of the benefits of SwiftData, as most apps define a number of custom types that model the data it creates or consumes, and SwiftData lets you persist that data so it’s available across app launches, and make use of the framework’s integration with SwiftUI to refetch that data and display it onscreen.

To let SwiftData save instances of a model class, a developer would import the framework and annotate that class with the Model macro. The macro updates the class with conformance to the PersistentModel protocol, which SwiftData uses to examine the class and generate an internal schema. Additionally, the macro enables change tracking for the class by adding conformance to the Observable protocol.

CloudKit syncing is supported. Data can be stored in files using DocumentGroup and synced via iCloud Drive, or you can use CloudKit to sync data between devices.

SwiftData is available in preview now.

swiftlogo

More Information

SwiftData Documentation

Related Articles

Apple Releases Swift Playgrounds 4

Swift 5.5 Adds Concurrency Support

Swift 5.2 Improves Performance

Apple Launches Swift Playgrounds

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Rust's Rapid Rise on TIOBE Index
10/07/2024

Rust is making spectacular progress up the TIOBE index and JavaScript is also on the up and experiencing a personal best. Kotlin is maintaining its inclusion in the top 20 and the gap at the very top  [ ... ]



Chainguard Images Now Come With JCK Certified Java
25/07/2024

Chainguard has released OpenJDK Java images which are JCK conformant, adhering to the strict Java standard but also with a twist. The twist is that the images are minimalist and based on Wol [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info