Core Data Tutorial - Lesson 1: Core Data Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Kaku welcome to the core data series in this lesson we're going to go through what core data is its benefits and how you're going to use it with Xcode first of all what is it core data is Apple's local object graph persistence framework now before I scare you away in simple terms it's a way for you to store data on your device locally and then retrieve it later for use the data stays locally on the device so it's not a solution for sharing data with other users and it's not a solution for syncing data across multiple devices although some of that can be done when you combine core data with cloud kit next why should we even use core data well assuming you need to store data in your app some benefits of using core data include number one it's a first party apple framework so it placed nicely and has a tighter integration with other apple api's and frameworks number two being a first party framework means that apple is going to make sure that any new technology it releases will work with it at least I'd like to think that Apple would make a bigger effort to make sure that core data plays nicely with anything that they release number three you don't have to install any third-party SDKs which means you don't have to work with cocoa pods or trying to keep third-party code libraries up to date all right now for some drawbacks of using core data number one it's not a remote database solution although syncing across devices should be able to be done with cloud kit to be honest I haven't looked into this very much myself yet number two the classes and methods to work with core data may be confusing at first so there's a little bit of a learning curve but I hope in this video series I can simplify that for you number three you have to retrain your brain to think of core data as an object graph persistence framework object graph persistence framework let's take a look at what that means it all has to do with how you interact with core data you see with traditional databases you explicitly insert create update and delete data from the database now even though under the hood core data is using an SQLite database you don't have to explicitly tell core data to insert create update or delete you just create and work with objects in your app like normal and behind-the-scenes core data we'll manage the data persistence for you let's take a look at a diagram to see how this works okay so let's say you have two classes family and person the person class contains some properties and the family class has a property storing the person objects for that family you create a family object in several person objects and then you relate those person objects to the family object now you want to store it in core data how do you do that well there are a few core data components that are needed one is the persistent container you can think of this as a representation of the core data store or database however your objects don't interact with the persistent container directly there's a layer on top of the persistent container called the managed object context think of this as sort of a Data Manager layer your objects will go through the managed object context to be stored or retrieved from the persistent store now these objects in memory along with how they relate to each other is called an object graph when you store the objects into core data all of the data in the properties along with the relationships are preserved in other words the object graph when you retrieve them from core data back into memory you can get them back in the same state they were in before so that's why they call core data an object graph persistence framework now to complete our understanding of how core data works let's revisit our diagram in previous lessons I've shown you how to work with json and decode them into useable objects in our app this process of changing the data into a different format and back is known as encoding and decoding or serializing and deserializing while core data needs to do the same thing here it serializes the object into a format that can be stored in the underlying SQLite database and then it'll deserialize it back into the objects of memory the code or functionality to do that serializing and deserializing process is with a class called ns-managed object so if you want your class to be able to be captured in core data the subclass ns-managed object that gives objects of your custom class the ability to be stored with core data okay so now your objects can be serialized and stored with core data however when you want to bring that object back from core data how does it know what format to deserialize that data back into in other words if you're trying to bring your person back how does it know about your person class and what properties your person class contains well here's the final piece of the core data puzzle in this example we use the person class that subclasses and s managed object well you don't actually create and write this person class yourself instead there's a visual editor where you have to define the class in the core data model they call the class an entity in the properties of your class are called attributes of that entity then after you define the entity and attributes in other words your class and its properties you generate the Swift classes from this core data model file it's just the command you run from an xcode menu and they'll generate the class files for what you've defined in the core data model the generated class will automatically be a subclass of NS managed object so that core data can serialize and deserialize it then you use that generator to class like you would any other class and core data can now store objects of that class and bring them back when needed so let's do a quick recap of how we're going to work with core data you define your entities and attributes in the core data model then you generate your classes from the core data model then you get a reference to the core data persistent container from the persistent container you get a managed object context and through that managed object context you can create objects and store them in core data for retrieval for later use now core data has a stigma of being hard to understand and use I hope I've missed a fight it at least a little bit for you in this video if you are able to follow this video and understand how core data works please confirm with me by leaving a quick comment below lastly if you enjoyed this video please give it a thumbs up it lets YouTube know that this is a great video and it will help the video get more Bossier so thank you for that in the next lesson we'll do a real example of using core data in Xcode alright I'll see you there
Info
Channel: CodeWithChris
Views: 39,447
Rating: undefined out of 5
Keywords: core data, coredata, core data swift, core data swift 5, core data relationship, core data tutorial, core data 2020, core data ios, core data apple, core data vs sqlite, when to use core data, coredata entities, core data database, what is core data, app development, database, swift programming, ios development, codewithchris, code with chris
Id: 6XASUd7h5-s
Channel Id: undefined
Length: 6min 47sec (407 seconds)
Published: Tue Jun 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.