SwiftData - The new way to handle persitance
2023-06-12
SwiftData is a lightweight and easy-to-use framework that simplifies data persistence in
Swift applications. It abstracts the complexities of interacting with databases and provides
a high-level API to perform common operations like storing, querying, and retrieving data.
SwiftData supports various database engines, including SQLite, Core Data, and Realm,
allowing you to choose the one that best suits your application's needs.
What is SwiftData?
SwiftData is a lightweight and easy-to-use framework that simplifies data persistence in
Swift applications. It abstracts the complexities of interacting with databases and provides
a high-level API to perform common operations like storing, querying, and retrieving data.
Sample Code: Storing and Retrieving Data
Let's dive into some sample code that demonstrates how to store and retrieve data using SwiftData with SQLite as the database engine. The code snippets provided below showcase a simple user management scenario.
Step 1: Define the Database Schema
Before we can store user data, we need to define the database schema. In this example, we will create a table called "Users" with columns for the user's name and email:
import SwiftData
@Model
final class User {
let name: String
let email: String
init(name: String, email: String) {
self.name = name
self.email = email
}
}
Step 2: Insert and Delete Data
Now, let's insert a user into the database and delete:
private func addUser() {
withAnimation {
let newItem = User(name: "John Wick", email: "info@icloud.com")
modelContext.insert(newItem)
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(users[index])
}
}
}
Step 3: Query Data and Visualise
To retrieve all users from the database, we can use the following code:
@Query private var users: [User]
// in the body property
List {
ForEach(users) { user in
VStack {
Text(user.name)
.font(.body)
.frame(maxWidth: .infinity, alignment: .leading)
Text(user.email)
.font(.caption2)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.onDelete(perform: deleteItems)
}
Source code
// ContentView.swift
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var users: [User]
var body: some View {
NavigationView(content: {
List {
ForEach(users) { user in
VStack {
Text(user.name)
.font(.body)
.frame(maxWidth: .infinity, alignment: .leading)
Text(user.email)
.font(.caption2)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addUser) {
Label("Add User", systemImage: "plus")
}
}
}
})
}
private func addUser() {
withAnimation {
let newItem = User(name: "John Wick", email: "info@icloud.com")
modelContext.insert(newItem)
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(users[index])
}
}
}
}
// SwiftData_WWDCApp.swift
@main
struct SwiftData_WWDCApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: User.self)
}
}