Learn Swift and iOS Development
Master iOS development through in-depth tutorials and comprehensive courses on Swift, SwiftUI, UIKit, Core Data, and more.
Master iOS development through in-depth tutorials and comprehensive courses on Swift, SwiftUI, UIKit, Core Data, and more.
5:45
In the previous episode, you learned how to load seed data from a file in the application's bundle. It's a common option that isn't that hard to implement. Once you've laid the groundwork, though, the seed data can come from anywhere. Let me show you what changes we need to make to seed the persistent store with data fetched from a remote server.
in Core Data
8:21
Hard-coding seed data is quick and easy, but it's not my preferred solution. Loading seed data from a file is a strategy I like more. The idea is simple. You include a file with seed data in the application bundle. The file is loaded at runtime and its contents parsed. The contents is used to seed the persistent store with data.
in Core Data
8:12
Every time we launch Notes, the persistent store is seeded with data. That's not what we want as it results in duplicates. In this episode, you learn how to easily avoid duplicates and how to start with a clean slate if you decide you no longer need seed data.
in Core Data
8:34
We successfully seeded the Core Data persistent store with hard-coded seed data in the previous episode. While the implementation works fine, it isn't perfect. Seeding the persistent store takes place on the main thread. We invoke the seed() method in the viewDidLoad() method of the NotesViewController class and we insert the managed objects into the main managed object context of the Core Data manager.
in Core Data
9:52
Seeding a Core Data persistent store with hard-coded seed data is fast and easy. The application we'll be seeding with data in this episode is Notes, the application we build in Core Data Fundamentals. Download the starter project of this episode if you'd like to follow along.
in Core Data
5:11
Seeding an application with data can be helpful for a wide range of reasons, including unit and performance testing. As a developer, seeding an application is simply convenient during development. You want to see and experience what the application feels like with data in it, without having to enter that data in by hand. How does the application perform with hundreds or thousands of records? What is the user experience like?
in Core Data
8:43
Developers often complain that Core Data has an arcane syntax and complicated API. "It's tedious to work with Core Data." seems to be the general consensus. It's true that Core Data used to be difficult to use and the framework's syntax wasn't as elegant as it could be. That's something of the past, though. The more Core Data matures, the more I enjoy and appreciate the framework. First impressions are difficult to change and it's therefore unsurprising that developers often fall back to third party libraries. Using a third party library to interact with a first party framework isn't something I recommend. Many of us find fetching records from a persistent store to be clunky and tedious. Is that true? In this series, I'd like to show you how easy and elegant fetching records from a persistent store can be.
in Core Data
5:06
It's time to shift gears and focus on the Category class. This shouldn't be difficult since you already know how to write unit tests for a Core Data model and we laid the foundation when we implemented the unit tests for the Note class. Pause the episode for a moment and give it a try.
in Core Data
11:19
Let's start with the updatedAtAsDate computed property. Change the name of the unit test to testUpdatedAtAsDate_NotNil(). We need to implement a second unit test for the updatedAtAsDate computed property if the updatedAt property doesn't have a value.
in Core Data
5:59
It's time to write the first unit test for the Note class. The first unit test we write tests the updatedAtAsDate computed property of the Note class. Remember that we only unit test the behavior we add to the Note class. We name the unit test testUpdatedAtAsDate()
in Core Data
7:02
Remember from Core Data Fundamentals that we need a managed object context to instantiate a managed object. To freshen up your memory, this is what it takes to create a note.
in Core Data
4:11
In this series, I show you how to unit test Core Data models. Before we start, though, I'd like to challenge you. How would you unit test a Core Data model? Pause the video for a moment and try to come up with a strategy. Remember what we covered in Core Data Fundamentals. Which ingredients do we need to create an NSManagedObject instance?
in Core Data
4:27
My hope is that this series has taught you that Core Data isn't as difficult as many developers believe it to be. Core Data isn't complex once you understand how the various pieces of the framework fit together.
in Core Data
8:01
In this episode, I'd like to discuss a concept that often confuses developers new to Core Data, faulting. Before I explain what faulting is, I want to show it to you.
in Core Data
10:21
The CoreDataManager class is in charge of the Core Data stack of the application. It encapsulates the Core Data stack and only exposes the main managed object context to the rest of the application.
in Core Data
1:10
Because we updated the CoreDataManager class in the previous episode, we need to make a few changes in the project.
in Core Data
7:13
It's time to refactor the CoreDataManager class. Let me walk you through the changes we need to make. Don't worry, though, most of the implementation of the CoreDataManager class remains unchanged.
in Core Data
8:37
We currently use one managed object context, which we created in the CoreDataManager class. In the application, we pass the managed object context to the objects that need it. This works fine, but there will be times when one managed object context won't cut it.
in Core Data
7:03
The last feature I want to add is the ability to tag notes. This feature is interesting because of the Many-To-Many relationship of the Note and Tag entities. In this episode, you learn how to work with such a relationship.
in Core Data
6:54
In the previous episode, we exposed the root cause of the crash we ran into earlier. The solution is versioning the data model.
in Core Data
6:43
An application that grows and gains features also gains new requirements. The data model, for example, grows and changes. Core Data handles changes pretty well as long as you play by the rules of the framework.
in Core Data
6:17
In this episode, I'd like to add the ability to assign a color to a category. This makes it easier to visualize which category a note belongs to.
in Core Data
8:16
To help users manage their notes, it's helpful to allow them to categorize their notes. Earlier in this series, we added the Category entity to the data model. Remember that a note can belong to only one category, but a category can have many notes. In other words, the Note and Category entities have a One-To-Many relationship. The data model editor visualizes this.
in Core Data
6:13
To update the table view, we listened for notifications sent by the managed object context of the Core Data manager. This is a perfectly fine solution. But it can be messy to sift through the managed objects contained in the userInfo dictionary of the notification. In a complex Core Data application, the NSManagedObjectContextObjectsDidChange notification is sent very frequently. It includes every change of every managed object, even the ones we may not be interested in. We need to make sure we only respond to the changes of the managed objects we are interested in.
in Core Data
7:03
The application now supports creating, reading, updating, and deleting notes. And this works fine. But Core Data has another trick up its sleeve.
in Core Data
1:38
The application currently supports creating, reading, and updating notes. But it should also be possible to delete notes. The pieces we need to add the ability to delete notes are already present in the notes view controller. In fact, we only need to implement one additional method of the UITableViewDataSource protocol to add support for deleting notes.
in Core Data
8:32
The application we're building wouldn't be very useful if it didn't include the ability to edit notes. Would it? If the user taps a note in the notes view controller, they should be able to modify the title and contents of the note.
in Core Data
8:22
In this episode, we fetch the user's notes from the persistent store and display them in a table view. The notes view controller is in charge of these tasks.
in Core Data
3:17
You may be wondering why we didn't save the note immediately after creating it. That's a fair question. We could have. But why would we? Why would we push the changes of the managed object context to the persistent store every time something changes in the managed object context? That's a waste of resources and it may even impact performance, depending on the complexity of the operation.
in Core Data
4:00
In the next episodes, we add the ability to create, read, update, and delete notes. Before we start, we need to make some preparations.
in Core Data
3:53
In the previous episode, we used the NSManagedObject class to represent and interact with records stored in the persistent store. This works fine, but the syntax is verbose, we can't take advantage of Xcode's autocompletion, and type safety is also an issue.
in Core Data
8:44
Core Data records are represented by the NSManagedObject class, a key class of the framework. In this episode, you learn how to create a managed object, what classes are involved, and how a managed object is saved to a persistent store.
in Core Data
6:41
Core Data is much more than a database and this becomes clear when you start working with relationships. Relationships in Core Data are powerful because the framework does a lot of the heavy lifting for you.
in Core Data
6:36
Core Data is great at managing relationships. In this episode, we continue our exploration of the data model by taking a close look at relationships.
in Core Data
5:39
The data model is a key component of the Core Data stack and an integral part of a Core Data application. In this episode, we explore the data model and learn about entities and attributes.
in Core Data
3:13
I'm not going to lie. I don't like singletons. Singletons are fine if they're used correctly, but I don't like singletons for convenience. They almost always lead to problems down the line. This means that the Core Data manager isn't going to be a singleton.
in Core Data
8:31
It's time to write some code. Had we checked the Use Core Data checkbox during the setup of the project, Xcode would have put the code for the Core Data stack in the application delegate. This is something I don't like and we won't be cluttering the application delegate with the setup of the Core Data stack.
in Core Data
1:06
Before we set up the Core Data stack, we need to create the project for Notes. Open Xcode and create a new project based on the Single View App template.
in Core Data
6:46
Earlier in this series, you learned what Core Data is and isn't. This episode zooms in on the building blocks of the Core Data framework.
in Core Data
1:49
Notes is a simple application for iOS that manages a list of notes. It's a fine case study to teach you the fundamentals of the Core Data framework. In this episode, I'd like to show you what Notes is capable of.
in Core Data
5:08
Developers new to Core Data usually don't take the time to learn the framework. Not knowing what Core Data is, makes it hard and frustrating to wrap your head around the ins and outs of the framework. I'd like to start by spending a few minutes exploring the nature of Core Data and, more important, explain to you what Core Data is and isn't.
in Core Data
3:08
Welcome to Core Data Fundamentals. In this series, you'll learn the ins and outs of Apple's popular Core Data framework. Even though we'll be building an iOS application, the Core Data framework is available on iOS, tvOS, macOS, and watchOS, and the contents of this series apply to each of these platforms.
in Core Data
Developers often complain that Core Data has an arcane syntax and a complicated API. "It's tedious to work with Core Data." seems to be the general consensus. It's true that Core Data used to be difficult to use and the framework's syntax wasn't as elegant as it could be. That's something of the past, though. The more Core Data matures, the more I enjoy and appreciate the framework.
in Core Data
Core Data is a fantastic framework and I love using it. I agree that Core Data has a learning curve, but isn't this true for many other frameworks? Because of Core Data's learning curve, developers often turn to third party libraries that make working with the framework easier.
in Core Data
The types you can store in a Core Data persistent store are limited and it probably won't surprise you that UIColor objects are not supported out of the box.
in Core Data
In Core Data Beyond the Basics, we work with an application that manages clients and their invoices. I named the application Invoices. What's in a name? The application is similar to, for example, the mobile application of FreshBooks.
in Core Data
If you're reading this, then I assume you are new to Core Data. You may have heard about Core Data and you'd like to find out whether it's a good fit for you or the project you're working on. If this description fits you, then take a seat. This Core Data tutorial teaches you everything you need to know to better understand what Core Data is and isn't.
in Core Data
In the previous tutorial, we discussed Core Data in the light of concurrency. I hope that lesson has taught you that Core Data and concurrency can go hand in hand. As long as you remember the basic rules we discussed, you should be fine.
in Core Data
Up to now, we've used a single managed object context, which we created in the CoreDataManager class. This works fine, but there will be times when one managed object context won't suffice.
in Core Data
The NSFetchedResultsController class is pretty nice, but you probably aren't convinced yet by what you learned in the previous tutorial. In this tutorial, we explore the NSFetchedResultsControllerDelegate protocol. This protocol enables us to respond to changes in the managed object context the fetched results controller monitors. Let me show you what that means and how it works.
in Core Data
The NSFetchedResultsController class isn't an essential component of a Core Data application, but it makes working with collections of managed objects much easier. This tutorial introduces you to the, almost magical, NSFetchedResultsController class.
in Core Data
Up until now, we've used NSManagedObject instances to represent and interact with records stored in the persistent store. This works fine, but the syntax is verbose, we lose autocompletion, and type safety is also an issue.
in Core Data
An application that grows and gains features also gains new requirements. The data model, for example, grows and changes. Core Data handles changes pretty well as long as you play by the rules of the framework.
in Core Data
The final piece of the CRUD puzzle covers deleting records from a persistent store. I can assure you that deleting records is no rocket science. We also take a closer look at the NSFetchRequest class. In the previous tutorial, we used this class to fetch the records of an entity. But NSFetchRequest has a lot more to offer.
in Core Data
In the previous tutorial, we created a list record and pushed it to the persistent store by saving its managed object context. The persistent store coordinator handled the nitty-gritty details of inserting the list record into the persistent store. This tutorial focuses on reading and updating records.
in Core Data
In this tutorial, we take a look at the NSManagedObject class, a key class of the Core Data framework. You learn how to create a managed object, what classes are involved, and how a managed object is saved to a persistent store.
in Core Data
Core Data is great at managing relationships. In this tutorial, we explore the data model and take a close look at relationships.
in Core Data
The data model is a key component of the Core Data stack and an integral part of every Core Data application. In this tutorial, we explore the data model and learn about entities and attributes.
in Core Data
In the previous tutorial, we set up a Core Data stack using the NSPersistentContainer class. While this is convenient, it teaches you very little about the Core Data stack and how to set one up from scratch. In this lesson, we create a Core Data stack without relying on Xcode's Core Data template.
in Core Data
In the previous tutorial, you learned about the Core Data stack and the classes involved. In this tutorial, we take a close look at Xcode's template for Core Data applications.
in Core Data
If you dipped your toe in iOS or macOS development, then you've probably heard about Core Data. Core Data is Apple's framework for managing and persisting an object graph. It was introduced in 2005, alongside the release of Mac OS X Tiger. On iOS, the framework has been available since iPhone OS 3.0.
in Core Data
NSManagedObject is the class you interact with most when you work with Core Data. The class may appear as a glorified dictionary, but it is much more than that. In this tutorial, I show you three features of the NSManagedObject class you may not know about.
in Core Data
Core Data received an important update during this year's WWDC. We already explored the addition the of the NSPersistentContainer class and Xcode's improved support for Core Data. But there are several more subtle additions and enhancements worth exploring, such as the NSPersistentStoreDescription class. That is the focus of this tutorial.
in Core Data
What is the difference between Core Data and SQLite? Even though it's a common question, it is the wrong one to ask. Comparing Core Data and SQLite is like trying to compare apples and oranges. But why is that?
in Core Data
Earlier this month, I wrote about Core Data faults. Faulting is a key concept of the Core Data framework. It is because of faulting that Core Data is as performant as it is and it ensures Core Data's memory footprint remains at an acceptable level.
in Core Data
Developers familiar with Core Data don't consider the framework difficult or hard to work with. If you respect the rules of the framework, Core Data won't surprise you. But that is often the problem. Developers make mistakes because they violate a rule they don't know about. In this article, I discuss three common mistakes developers make working with Core Data.
in Core Data
Core Data is one of the most misunderstood frameworks available on iOS, tvOS, macOS, and watchOS. And that is unfortunate because it is a fantastic piece of software engineering. But why is it misunderstood?
in Core Data
This tutorial zooms in on an important topic for anyone working with Core Data, faulting. Faulting is a concept that often confuses developer new to Core Data. Before I explain what faulting is, I want to show it to you.
in Core Data
You are reading this tutorial, which means you are interested in learning more about the Core Data framework. It probably also means that you have heard a word or two about the Core Data stack. But what is it? And what is its role in a Core Data application? In this tutorial, you learn everything you need to know about the Core Data stack.
in Core Data
Last week I asked on Twitter why people choose Realm over Core Data. The answers are both surprising and interesting. I would like to dive a little deeper into the comparison between Core Data and Realm. While I have used Core Data for close to ten years, I don't have much experience with Realm. With this article, I would like to debunk a few misunderstandings about Core Data and point out some less obvious advantages of Core Data and Realm. I don't intend to promote or favor one or the other. That is not the goal of this article.
in Core Data
Developers are often confused by one-to-many and many-to-many relationships in Core Data, especially when working with Swift. The truth is, with Xcode 8 and Swift 3, it has never been easier to work with Core Data relationships. Let me show you how.
in Core Data
From the moment you start working with relationships, it becomes evident that Core Data is not a database. It is much more than that. Delete rules are a fine example of the framework's strength and flexibility. They are one of the conveniences that make working with Core Data enjoyable.
in Core Data
In this tutorial, we add sections to the table view of quotes. Each section contains the quotes of a particular author. If you want to follow along, download the source files of this tutorial at the bottom of the tutorial.
in Core Data
In this tutorial, we add the ability to update quotes. Not only do we want the table view to reflect the changes we make to a quote, we also want to make sure the sort order of the quotes is updated when a quote is modified. You guessed it. The NSFetchedResultsController class and the NSFetchedResultsControllerDelegate protocol make this almost trivial.
in Core Data
In yesterday's tutorial, we populated a table view with quotes using the NSFetchedResultsController class. But the table view is currently empty since we haven't added the ability to add quotes yet.
in Core Data
Even though you don't need to use the NSFetchedResultsController class to populate a table view, I find myself using it quite a bit in Core Data applications. Swift 3 has substantially improved support for Core Data and the NSFetchedResultsController class also benefits from these optimizations.
in Core Data
In June of this year, Apple revealed an impressive list of additions and improvements to the Core Data framework. The NSPersistentContainer class is one of the additions developers have been asking for since the framework was introduced more than a decade ago. The NSPersistentContainer class encapsulates the Core Data stack. Setting up and managing a Core Data stack has never been easier.
in Core Data
Where do you store the persistent store of your Core Data application? If you are using the Core Data stack Xcode created for you, then chances are that you haven't changed the location of the persistent store. In this tutorial, I show you where you can keep the persistent store of your Core Data application and I highlight the pros and cons of each option.
in Core Data
The Core Data stack we have built in this series has grown quite a bit in complexity. But if you have a good grasp of the framework, it isn't rocket science. In this lesson, we add the last piece of the puzzle. You can follow along by downloading the repository from GitHub.
in Core Data
A common mistake developers new to Core Data make, is not taking the time to learn the basics of the framework. If you don't know what Core Data is, it is very hard to understand the ins and outs that make the framework tick. In this post, I would like to spend a few minutes exploring the nature of Core Data and, more important, finding out what Core Data is and is not.
in Core Data
The Core Data stack we have built in this series is slowly taking shape. With every iteration, we add a dash of complexity in return for a few key advantages.
in Core Data
Previously, we updated the Core Data stack by adding a private managed object context. Remember that the private managed object context performs two tasks. It pushes its changes to the persistent store coordinator. It acts as the parent of the main managed object context.
in Core Data
Have you ever considered using more than one managed object context in a Core Data application? In this installment of Building the Perfect Core Data Stack, we explore the options you have and the benefits of a Core Data stack with multiple managed object contexts.
in Core Data
In this series, we explore several techniques and best practices for building a robust, modern Core Data stack. Modern? Yes. Core Data has undergone several important changes in the past few years. In this series, we discuss what a modern Core Data stack looks like.
in Core Data
It is no secret that I love the Core Data framework. It does what I want it to do, mostly, and it continues to evolve over time. This year, Apple introduced several amazing additions and improvements that everyone working with Core Data is going to appreciate.
in Core Data
It is very common for people new to Core Data to be overwhelmed by the framework's terminology. Core Data is a framework you cannot start working with without a basic understanding of what makes the framework tick.
in Core Data
Even though Core Data is pretty performant, some operations can take a while to complete. When such an operation is performed on the main thread, it blocks the main thread. The result is an unresponsive user interface, which you want to avoid at any cost.
in Core Data
Core Data is a fantastic framework and I love using it. The framework is sometimes accused of being slow and performing badly with large data sets, but the truth is that the developer is usually the one to blame.
in Core Data
A question I hear surprisingly often is how to remove every record of a Core Data entity. This is a valid question and it sounds trivial. Up until iOS 9 and OS X El Capitan, though, it wasn't that trivial at all. Because not every application requires iOS 9 or OS X El Capitan, I would like to start this tutorial by explaining how to solve the problem the old way.
in Core Data
Have you ever wondered how the NSFetchedResultsController class does its magic? A fetched results controller seems to be magically notified whenever the result of its fetch request changes. The Core Data framework exposes several APIs that allow developers to replicate the behavior of the NSFetchedResultsController class.
in Core Data
Apple's persistence framework is sometimes said to have a bad reputation. Is that warranted? Is Core Data difficult to use? Does it have a steep learning curve? Is it a dated persistence solution?
in Core Data
Is Core Data thread safe? Yes and no. Even though the framework originated in a time where computers with multicore processors were rare, Core Data is designed to operate in a multithreaded environment. In other words, it works fine on the latest iPhone and a Mac Pro with 28 cores.
in Core Data
If Core Data is slow, then you are most likely using a very big data set or not correctly using the framework. The latter is the most common issue. Core Data was designed with speed and efficiency in mind. It only fetches the data your application needs and fills in the gaps as your application asks for more data.
in Core Data
This question keeps popping up time and time again and it's a good question if you are new to Core Data. What you need to know is that Core Data is not a database. That said, when working with Core Data it acts as a relational database hence the confusion.
in Core Data
Does Core Data encrypt the persistent store it manages? The answer is yes and no. Core Data doesn't encrypt the data you store in the persistent store, but it is possible to enable encryption. Let me explain what that means and how it works.
in Core Data
Apple's persistence framework was introduced more than a decade ago so it's shouldn't surprise you that it is available on every platform, iOS, tvOS, macOS, iPadOS, watchOS, and Mac Catalyst.
in Core Data
In the previous episode of Adding Core Data to an Existing Swift Project, we saved data to the persistent store, a SQLite database. In this episode, we fetch the books stored in the persistent store and update the label of the books view controller.
in Core Data
In the previous episode of Adding Core Data to an Existing Swift Project, we added a data model to the project and set up the Core Data stack, using the NSPersistentContainer class. In this episode, you learn how to save data to a persistent store.
in Core Data
Core Data is Apple's widely used persistence framework. It has been around for more than a decade and has earned its stripes. Getting stared with Core Data isn't as difficult as many developers make you believe as long as you take the time to learn the basics. In this episode, I show you how to add Core Data to an existing Swift project.
in Core Data
Most developers don't worry where Core Data stores its data and many probably don't know where the persistent store is located in the application's sandbox. To debug Core Data issues, however, it can be useful to access the persistent store and inspect its data. In this post, I show you where you might find the data Core Data stores.
in Core Data
Core Data has been around for more than a decade and it has grown into a robust persistence solution. I have used Core Data in dozens of applications and enjoy working with the framework. Some developers swear by Core Data while others passionately dislike it.
in Core Data