(iOS) Core Data Tutorial — Part 1
Terminology
This tutorial is consists of two part. In part 1, I will introduce you the core concepts related to Core Data. In part 2, we are going to build a ToDo app using Core Data.
Core Data
Core Data is a framework that you use to manage the model layer objects in your application. Using Core Data, you can save your application’s permanent data for offline use and cache temporary data. Notice Core Data is just a framework so that we can work with the database easily. It is not the database itself.
Entity
An entity is a basic Core Data class. It is very similar to the concept of the table in SQL, however, it encapsulates much more information.
Managed Objects
The NSManagedObject
class is the base class which represents any Core Data entity/object. A managed object is associated with NSEntityDescription
that provides metadata related to the object such as the name of the entity that the object represents and the names of its attributes and relationships. A managed object is also associated with a managed object context that tracks changes to the object graph.
It has the basic functionality of notifying you when the object has changed or is about to be deleted or saved. The managed object is consists of Core Data class and Core Data Properties.
Core Data Class
It holds your own custom code. In the project, it is represented as the CoreDataClass.swift
file. This file is only generated if there is not a version present.
Core Data Properties
Core Data properties are generated every time the Core Data classes are generated. In the project, it is represented as the CoreDataProperties.swift
file.
NSManagedObjectContext
In order to save the NSManagedObject instance or its subclass, you need to have NSManagedObjectContext. It is an object space that you use to manipulate and track changes to managed objects such as adding, updating and removing. Changes to managed objects are held in memory, in the associated context, until that context is saved to one or more persistent stores. Since all your data operations have to go through the managed object context. Therefore, all NSManagedObjects have to have an associated context.
NSPersistentContainer
You can think of the persistent container as the representation of your Core Data Stack in your app. It represents your data store. You have to ask the persistent container for a managed object context which you use to save your managed objects.
Attribute Types
We can use primitive types for attributes. Every attribute starts off as undefined. Undefined attributes must be marked as transient which is simply an NSObject. In addition to primitive types, there are two kinds of types.
Binary Data
As you add larger data sets such as image data to Core Data, it will slow down the access time. Using the Binary Data type, you can select an option that allows us to use external storage. When we check this option, Core Data saves a reference to data. Data itself is saved outside of the app.
Transformable
Transformable is a type that converts to an NSObject and then is saved as an NSData. You have to perform a cast operation to access the object.
NSFetchResultController
NSFetchedResultsController is a controller that you use to manage the results of a Core Data fetch request and to display data to the user. Using the controller, you can do the following things.
- Fetch necessary data instead of fetching all data at once
- Notified of changes
It can be notified when the data for the associated managed object context has been changed. - Cache the data (Optional)
It can cache the data so that the app can display the contents without fetching again. - Order the data by sections
This feature allows us to display the data separated by the given attribute.
Relationships
Relationships allow you to connect objects to other objects in a structured manner. There are two kinds of relationships: one to one and one to many.
That’s everything for part 1! In part 2, we are going to learn how these concepts work in an app by integrating Core Data into a ToDo app!
Reference
Apple Developer Documentation — Core Data:
https://developer.apple.com/documentation/coredata
Apple Developer Documentation — NSFetchedResultsController:
https://developer.apple.com/documentation/coredata/nsfetchedresultscontroller
Apple Developer Documentation — NSManagedObjectContext:
https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext
Apple Developer Documentation Archive — Core Data Programming Guide:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/
Core Data Overview By Daniel Eggert:
https://www.objc.io/issues/4-core-data/core-data-overview/
iOS Programming 6th Edition by The Big Nerd Ranch Guide:
https://www.bignerdranch.com/books/ios-programming/
iOS 11 and Swift 4 for Beginners: 200+ Hands-On Tutorials:
https://www.udemy.com/ios-and-swift-for-beginners-200-hands-on-tutorials/learn/v4/overview