Saving Data with Core Data in iOS 12, Xcode 10, and Swift 4.2 – raywenderlich.com

Saving Data with Core Data in iOS 12, Xcode 10, and Swift 4.2 – raywenderlich.com


(electronic music) Hey, what’s up everybody? This is Brian, and in this video tutorial series we’ll be diving into the wonderful world of Core Data. Okay, let’s face it. No matter what type of application you work on, you will probably run into the need for persisting data. Sometimes this might be a case where you need to persist just a few bits of information, such as a couple of user settings, or it could be that your application requires saving huge amounts of data so you need a full-fledged data persistence mechanism. Or it could be somewhere in between these two extremes. Now, if you simply need to save just a few bits of information, you can always use the built-in iOS UserDefaults system. In fact, some developers use this for saving more than just the occasional bits of data, though we would not recommend doing that. If you need a more flexible and robust data persistence mechanism, you, again, have a few different options, such as using SQLite, a custom data management system like Realm, or even Apple’s own Core Data. Each of these options has their own advantages and well, they have their own drawbacks as well. However, Core Data as a solution that is bundled in with iOS is something that every developer should pay attention to, at least to know enough to know if this is something that they would want to use in their application or not. Okay, so, down to the nitty gritty. What is Core Data? Apple positions Core Data as an object graph management system, and you’re probably wondering, what the heck does that mean? Well rather than being a simple database for storing data, Apple sees Core Data as a way to store your application objects and persist their state information. But if that helps, you can think of it as a database, but be warned. If you come from a database background this can result in some issues in understanding how you can work with Core Data if you persist in following that sort of viewpoint of Core Data. Okay, so back to the object graph side of things. How does this thing actually work? Well, all iOS apps use objects to represent elements in the app like a view, view controllers, UI elements, and et cetera. In addition to these, there are probably other objects which represents the data model in your application. For example, in an address book application each contact might be represented by an object. Going further, each object might encapsulate other additional objects. So, for example, several address objects which represent a particular contact’s home address, work address, phone number, and et cetera. Now say that your address book application has several contacts added to it. This data is represented by several of your contact objects, while your UI is created by your code and so will be recreated by the code in your application in the next time you run it, your data, unless it was persistent, would be lost when you exit your application. So Core Data provides a way for you to persist your contact objects as objects that can be immediately reused in your application when they are reloaded. To reiterate, when you save data to a normal database you have to retrieve the data and then load it into your application object in order to use it. With Core Data you save the object itself and then later reload it so you can keep using that object. So why should you actually use Core Data? Well Core Data historically has a bit of a reputation for being a bit cumbersome and complex to use. Perhaps because of this, in recent times other third party solutions have cropped up which try to improve upon Core Data. Either due to the emergence of these solutions or because they themselves came to understand the complexity of working with Core Data, Apple seems to have recently begun to revamp and streamline Core Data. Tasks that took a whole lot of boilerplate code previously have suddenly become just a couple of lines of code. So this is a good time to get in on learning Core Data. Additionally, being a first-party framework, Core Data has some inherent advantages. First, there are tools built into Xcode and instruments for developing using Core Data. Next, it’s a multi-platform solution available on both iOS and macOS. It can easily scale to gigabytes of data, and it’s optimized to use memory efficiently. Now our aim here is to give you an overview of Core Data so that you can understand how easy or difficult it is to work with. Once you’ve done that, you can perhaps take a look at some of our other tutorials on data persistence to see which particular approach works best for you. After all, each person’s gonna be having different requirements. Now this course is composed of two different parts. In this first part you’ll get up and running with Core Data itself. You’ll learn how to integrate Core Data into an existing project, and then you’ll learn about managed objects. Once you’ve worked with managed objects you’ll also work with attributes, so naturally you’ll need to know about all the various attribute types. Then you’ll learn about saving binary data, after which we’ll dive into filtering and wrap it up with sorting. In short, you’ll get up and running with the framework in no time. Now throughout this series you’ll be presented with a bunch of challenges. The challenges will take what you have learned and apply your knowledge in a real world situation. Do not skip over them. Consider them requirements. Now if you get stuck, our challenge videos will walk you through the solution, so give them your best shot. Okay, with all of that said, let’s dive into now the world of Core Data.