Chapter 18
Working with Database and SwiftData
Learn not to add too many features right away, and get the core idea built and tested.
– Leah Culver
Congratulations on reaching this milestone! By now, you have successfully developed a basic app that allows users to list their favorite restaurants. Up to this point, all the restaurants have been pre-defined in the source code and stored in an array. If you want to add a new restaurant, the simplest approach is to append it to the existing restaurants
array.
However, if you follow this method, the new restaurant data will not be permanently saved. Data stored in memory, such as an array, is volatile. Once you quit the app, all the changes will be lost. Therefore, we need to determine a way to persistently store the data.
To achieve permanent data storage, we need to save the data in a persistent storage medium such as a file or a database. By saving the data to a database, for example, the data will remain secure even if the app quits or crashes. On the other hand, files are more suitable for storing small amounts of data that don't require frequent modifications. Files are commonly used for storing application settings, such as the Info.plist file.
The FoodPin app may require storage for thousands of restaurant records, with users frequently adding or removing records. In such cases, a database is an appropriate solution for managing a large dataset. In this chapter, I will guide you through the SwiftData framework and demonstrate how to utilize it to handle database operations. We will cover topics such as creating the data model and performing CRUD (create, read, update, delete) operations using the SwiftData framework.
You will make a lot of changes to your existing FoodPin project, but after going through this chapter your app will allow users to save their favorite restaurants persistently.
What’s SwiftData
First and foremost, it's important to note that the SwiftData framework should not be confused with a database. Built on top of Core Data, SwiftData is actually a framework designed to help developers manage and interact with data on a persistent store. While the default persistent store for iOS is typically the SQLite database, it's worth noting that persistent stores can take other forms as well. For example, Core Data can also be used to manage data in a local file, such as an XML file.
Regardless of whether you're using Core Data or the SwiftData framework, both tools serve to shield developers from the complexities of the underlying persistent store. Consider the SQLite database, for instance. With SwiftData, there's no need to worry about connecting to the database or understanding SQL in order to retrieve data records. Instead, developers can focus on working with APIs and Swift Macros, such as @Query
and @Model
, to effectively manage data in their applications.
The SwiftData framework is newly introduced in iOS 17 to replace the previous framework called Core Data. Core Data has long been the data management APIs for iOS development since the era of Objective-C. Even though developers can integrate the framework into Swift projects, Core Data is not a native solution for both Swift and SwiftUI.
In iOS 17, Apple finally introduces a native framework called SwiftData for Swift on persistent data management and data modeling. It's built on top of Core Data but the APIs are completely redesigned to make the most out of Swift.
To access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.