Core Data Fundamentals with Swift

It's inevitable: you're going to need to persist data on a user's device. This course will give you the essential skills you need to architect an app to use Core Data. You'll learn to model, manage, and display data using Core Data in this course.
Course info
Rating
(24)
Level
Beginner
Updated
Mar 31, 2017
Duration
4h 39m
Table of contents
Course Overview
Starting Out with Core Data
Setting up the Core Data Stack
Creating a Data Model
Building Relationships Between Entities
Saving and Accessing Data
Showing and Synchronizing Data with NSFetchedResultsController
Making Changes to Core Data Models with Model Versions
Understanding Core Data Changes in iOS 10 and macOS Sierra
Description
Course info
Rating
(24)
Level
Beginner
Updated
Mar 31, 2017
Duration
4h 39m
Description

Managing data is crucial to building dynamic mobile apps. You won't be able to escape the need to save data to a user's device. When that need arises, Core Data can be a powerful tool to help solve the data persistence challenges of just about any app, no matter how big or small. In this course, Core Data Fundamentals with Swift, you'll learn how to set up and architect an app with Core Data so that it's maintainable and testable. First, you'll discover how to create a data model and explore how to save, access, and delete data. Next, you'll explore how to keep a user interface in sync with the data in your persistent store as users interact with your app. Finally, you'll see how to implement changes to your data model, even after your app is published to the App Store. When you're finished with this course, you'll have the foundation you need to start using Core Data in your apps today.

About the author
About the author

Andrew Bancroft is a born learner, passionate about discovery and about sharing what he’s learned with others. He has embraced Swift for iOS development since day zero, and loves empowering developers to succeed at building iOS apps with Swift.

More from the author
CloudKit Fundamentals
Beginner
3h 31m
Sep 20, 2018
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hello and welcome to Pluralsight. My name is Andrew Bancroft. I'm so glad to be able to spend some time with you exploring the fundamentals of Core Data with Swift. Dealing with data is impossible to avoid. Sooner or later, you're going to have to save data to a user's device. When that time comes, what will you do? What technology will you choose? Well, I hope that by the end of this course, you'll be as excited as I am to pull Core Data out of your toolbox to solve the data persistence challenges of your app. So what exactly will you learn in this course? Great question. I'll teach you how to set up and architect your app with Core Data, so that it's maintainable and testable. We'll do a little data modeling together and explore the all-important tasks of saving, accessing, and deleting data. I'll show you what it takes to keep a user interface in sync with the data in your persistent store, as users interact with your app. And since change is the only constant in life, I'll walk you through the strategy of implementing changes to your Core Data model, even after you've published it to the app store. This course is going to give you the foundation you need to start using Core Data in your apps today. All you need to get with this course is a little bit of experience working in the iOS SDK with Swift. If you need some help, Pluralsight's got you covered with excellent material on both iOS and Swift. I can't wait to take you on this journey to discover the power of Core Data for managing the data in your apps. So let's explore with Core Data Fundamentals with Swift at Pluralsight.

Creating a Data Model
Creating data models is where a lot of the fun begins. So far we've done a lot of the plumbing work, setting up the Core Data Stack. And while we have to do it, it's not necessarily the most fun part of Core Data. Data modeling, however, is where the practical part begins. In this module, I'm going to teach you about creating a Core Data model using Xcode's Data Model Designer. We'll use that designer to create entities, which you can think of as the nouns or the things that you'll persist in your data store. Entities have properties associated with them called Attributes. The word attribute is another keyword that you'll want to hold onto. Entities tend to map conceptually over to something like a class in Swift, or as attributes tend to map conceptually over to properties on a class. This module will look at that a little deeper, and we'll see some really practical examples of what that looks like in our ShoutOut app. Back when we were defining Core Data, I mentioned that it's a framework that handles graphs of objects. To be able to manage object graphs, Core Data needs the ability to build relationships between entities. This module I'm going to keep things simple, we're just getting started with data modeling, but rest assured that I will cover how to configure a relationship between two entities in module four, which is called Building Relationships Between Entities. The last thing I'll teach you in this module is how to make using some of these entities that you create in the data model designer easy to use in code by creating subclasses of a Core Data class called NSManagedObject.

Building Relationships Between Entities
Entities, attributes, and relationships. Well, so far most of our work has been around entities and attributes, but now it's time to talk about relationships. I have a few learning goals that I want to go over with you right now, things that I want you to be able to take away after you watch this module. If you're like me, you always want to know why. Why would I want to build relationships in my data model? Well, I've got a few reasons that we're going to go over right at the very beginning of this module. Then we'll move right on to talking about the kinds of relationships that can be created between entities. Once we've laid the groundwork there, I want to talk about what happens if you delete an instance of an entities from one side of the relationship or the other. How does Core Data handle this and keep the persistent store in a consistent state? And even more important, what kind of control do you as a developer have to influence how Core Data behaves in this situation? Toward the end of our time together in this module, I'm going to do a demo for you, and in that demo I'm going to build a new entity called Employee. And that seems about right, since the users of the app will be sending ShoutOuts to fellow employees. So once we have both the ShoutOut and the Employee entity set up, I want to show you how to build a relationship between those two so you see the mechanics in action.

Saving and Accessing Data
Saving, accessing, and deleting data are the topics of this module. I think you're going to really enjoy this segment of the course. We're finally getting to the persistence part of Core Data. Coming up, I'm going to introduce to you a few of the classes in the Core Data framework that we haven't talked about yet, but that we're going to really need when it comes to saving, accessing, and deleting data. In the discussion about saving data, I'm going to introduce to you the class called NSEntityDescription and what it's used for. The NSFetchRequest class is the Core Data framework type that I'm going to teach you about when it comes to the topic of accessing data. And, of course, mixed in there with accessing data is the concept of filtering and sorting it. You'll become familiar with the NSPredicate class for filtering data, and NSSortDescriptor for sorting data. There will be several demos that we'll expand on our ShoutOut app. By the end of the module, the editor screen will be ready to use for the purpose of saving a full-blown ShoutOut to the persistent store. I'm also going to teach you how to delete data that exists in the persistent store, and implement that function on the details screen. Join me now as we launch into the topic of saving data.

Showing and Synchronizing Data with NSFetchedResultsController
When you've gotten to the point of implementing a way for users to save data into the persistent store, well, the immediate question that you begin to ask is, how do I make sure that all of the screens in my apps stay up-to-date with the latest data that's in that persistent store? You immediately run into the challenge of showing that data in the UI, and then keeping that UI in sync with the changes that occur down in the persistent store. In this module, I'm going to walk you through two scenarios in the ShoutOut app. These two scenarios both require two slightly different approaches when it comes to keeping the UI in sync. Along the way, I'm going to each you about another Core Data framework type called NSFetchedResultsController. This particular class is really useful for keeping UI table views current with the persistent store state. I'm also going to teach you how to observe the NSManagedObjectContextDidSave notification. That's going to involve using the default notification center, and that's going to be sort of the second way to detect and respond to changes that are occurring in the persistent store. The demos are in this module are going to take us to a nearly feature complete app with just a couple of things left to enhance at the very end of the course. In a lot of ways, this module is going to put together almost everything that you've learned so far in the course. So are you ready to put your skills to the test? I'm going to begin with a short demo to get the stage setup so that the rest of the work we try to do in this module will go smoothly.

Making Changes to Core Data Models with Model Versions
Since you and I last talked, we were able to deploy the ShoutOut app to a pilot group of early beta testers. You know what that means, right? Yep, the inevitable has happened. We just got out of a meeting with the testers from Human Resources, and guess what, they have a few suggestions for us when it comes to the ShoutOut app prototype that we're building for them. It turns out that some of the changes they'd like to see will affect our data model. This is a pretty big deal. Changing things that far down in the stack need a little bit of extra thought and consideration. And that's what this module is all about. How to handle changes in the data model when an app is already deployed into the wild. I'll teach you how to create new versions of a data model, and then I'll cover how to migrate data from older versions of a data model to the latest version of that model, so that users don't experience any data loss as they update the app. In the demos, I'm going to walk you through the process of creating a new data model using Xcode's tooling, and then I want to show you how to enable Core Data's data migration feature. We'll work together to finalize the app by implementing our testers' feedback.

Understanding Core Data Changes in iOS 10 and macOS Sierra
About the only thing that never changes is the fact that things are always changing. Apple's platforms are no exception to this rule, but what I find relieving about Core Data is the fact that Apple has taken the initiative to introduce new features alongside of their Swift language and alongside their new platform releases. Some of these features are things that we've already been using, such as the generic typing of the standard Core Data framework types. We've been able to take advantage of those, because those rely on Swift 3 features, not on iOS 10 or MacOS Sierra features. There are a few things, though, that Apple introduced that require you to be targeting their latest platforms. There's a new framework type that helps simplify the Core Data Stack creation routine, and that type is called the NSPersistentContainer. I'm going to introduce that new class to you in this module. And then in the demo, I want to walk you through how to refactor things to take advantage of this new NSPersistentContainer class. I'll wrap everything up by recommending some additional resources to you to help you build on the fundamental skills that you've gained in this course.