Building a ToDo App with Core Data
One common question of iOS app development is how we can work with Core Data and SwiftUI to save data permanently in the built-in database. In this chapter, we will look into it by building a ToDo app.
Since the ToDo demo app makes use of List and Combine to handle the data presentation and sharing, I assume that you've read the following chapters:
- Chapter 7 - Understanding State and Binding
- Chapter 10 - Understanding Dynamic List, ForEach and Identifiable
- Chapter 14 - Data Sharing with Combine and Environment Objects
If you haven't done so or already forgot what Combine and Environment Objects are, go back and read the chapters again.
So, what are we going to do this chapter in order to understand Core Data? Instead of building the ToDo app from scratch, I already built the core parts of the app. That said, it can't save the data permanently. To be more specific, it can only save the to-do items in an array. Whenever the user closes the app and starts it again, all the data is gone. We will modify the app and convert it to use Core Data for saving the data permanently to the local database. Figure 1 shows some sample screenshots of the ToDo app.
No worries. Before we perform the modification, I will walk you through the starter project so you fully understand how the code works. Other than Core Data, you will also learn how to customize the style of a toggle. Take a look at the screenshots above. The checkbox is actually a toggle view of SwiftUI. I will show you how to create these checkboxes by customizing the Toggle's style.
We got a lot to cover in this chapter, so let's get started.
To access the full content and the complete source code, please get your copy at https://www.appcoda.com/swiftui.