How to Save and Manage Data with Core Data in Swift - SwiftUI Basics Tutorial 2023

Опубликовано: 29 Август 2023
на канале: Karin Prater
16,111
342

Are you struggling to find an efficient and easy way to save and manage user data in your Swift app? - In this tutorial, we’ll dive into the basics of Core Data, a powerful framework that allows you to save or cache data locally on the device.

In a previous tutorial, I created a macOS/iOS task manager app and encountered challenges with filtering data streams. To overcome this, we’ll transform the project from using dummy data defined in code to utilizing Core Data.

By following along with this tutorial, you’ll witness a direct comparison between using structs and Core Data, gaining valuable insights into the benefits of the latter.

⬇️ You can find project files here: https://github.com/gahntpo/CoreData

Core Data enables data persistence and caching. It even offers tools for syncing data across multiple devices, making it ideal for cross-platform apps. Over the years, Core Data has evolved, adding features like undo and redo functionality.

While Core Data has been around since iOS 3, it was primarily used with Objective-C. This historical context makes it slightly more challenging to work with in Swift. However, fear not! I’ll share some tricks to simplify the process. Although it requires initial setup, the effort is worthwhile. Once you’ve correctly defined your data models and written some helper code, building the UI in SwiftUI becomes a breeze.

Core Data is the mediator between your app and the database, handling file storage, and retrieval. You won’t need to worry about file management or other cumbersome tasks; Core Data takes care of the heavy lifting. To define data types, we use the visual editor in the .xcdatamodeld file. Additionally, we need to configure our app to use Core Data and define the schema and model definitions. We can even choose between a SQLite database or a temporary one, which is handy when working with SwiftUI previews. This allows us to experiment with different data without persisting it permanently.

00:00 Introduction
01:41 What is Core Data
06:02 Sample Project
10:39 Defining the Model for Task
25:37 Adding Inititalizer
28:40 What is the Core Data Stack
31:15 How to setup your Xcode project with Core Data Container
35:01 Xcode Preview with Core Data
38:29 Deleting Core Data objects
40:56 Fetching Data with NSFetchRequest
46:21 Working on the Sidebar view to use Core Data
52:19 Changing the Task List View
58:45 How to use Relationships
1:08:25 Adding Subtasks
1:18:57 How to make Core Data changes animated in Swiftui
1:19:40 Saving changes permanently in Core Data
1:22:22 Wrap up

If you liked what you learned and you want to see more, check out one of my courses!
👨‍💻 my SwiftUI course https://learn.swiftyplace.com/swiftui...
👨‍💻 my Core Data and SwiftUI course https://learn.swiftyplace.com/swiftui...

#coredata #swiftui #dataflow


Смотрите видео How to Save and Manage Data with Core Data in Swift - SwiftUI Basics Tutorial 2023 онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Karin Prater 29 Август 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 16,111 раз и оно понравилось 342 людям.