Core Data
Last updated
Was this helpful?
Last updated
Was this helpful?
Core Data is an object graph and persistence framework provided by Apple. It allows data to be organized in a relational entityβattribute model manner, so that it can be serialized into XML, binary, or SQLite stores.
You can use Core Data to save your applicationβs permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device.
π‘ Remember that Core Data isnβt a database nor it consists of table of rows and columns. Core Data abstracts the details of mapping your objects to a store or database, making it easy to save data from Swift and Objective-C without administering a database directly.
The first step in working with Core Data is to create a data model file to define the structure of your appβs objects, including their object types, properties, and relationships. You can add a Core Data model file to your Xcode project when you create the project, or you can add it to an existing project. Letβs build a core data model for a notes app.
While creating a new project, select the Use Core Data checkbox. The resulting project includes an .xcdatamodeld
file and the AppDelegate.swift
file with Core Data Stack code.
Choose File βNew βFile and select the iOS platform tab. Scroll down to the Core Data section, select Data Model, and click Next and save it to desired directory. Letβs save it in Models folder.
Xcode adds an MyNotes.xcdatamodeld
file in the Models directory.
The next step is to create a data-structure that will hold our notes. A Note
class look like this:
We will create a entity for this Note class. An entity
describes an object, including its name, attributes, and relationships. Create an entity for each of your app's objects.
Click Add Entity at the bottom of the editor area. A new entity with placeholder name Entity appears in the Entities list.
In the Entities list, double-click the newly added entity and rename it as Note
. This step updates both the entity name and class name visible in the Data Model inspector.
After you create an entity, you can add attributes to that entity. An attribute
describes a property of an entity. Add id
,text
,lastUpdated
as attributes to Note
entity.
Model, specify the classes that youβll use to create instances of your entities. Core Data optionally generates two files to support your class: a class file and a properties file.
To select a code generation option:
Select an entity from the Entities list.
In the Data Model inspector, below Class, the Codegen pop-up menu offers three options: Manual/None, Class Definition, and Category/Extension.
Choose Manual/None
and from the Xcode menu bar, choose Editor
-> create NSManagedObject Subclass
-> select MyNotes
as Data Model β select Note
as the entity to manage β choose Models
directory.
The Xcode will generate two files CoreDataClass and CoreDataProperties. CoreDataProperties file is sort of the representation of our data model(entity) which will have attributes and relationship property. CoreDataClass is a class that is available to us extend or add some properties to the Note class.
β While selecting
Manual configuration
, if we change any property toNote
entity, we manually have to configure those changes in these two files.β In
Class Definition
configuration, Xcode will automatically generate the required NSManagedObject subclass as part of the projectβs derived data. If we have to change anything, Xcode automatically syncs these files with the corresponding entity. But, we will not have access to use class file and extend its properties.β In
Category/Extension
configuration, Xcode will only automatically generate CoreDataProperties.swift for you and you will have to manage and maintain CoreDataClass.swift yourself. So, we can use this configuration without worrying about the entity properties.
While including a CoreData at project initialisation, you will see some extra code added there like persistent container in AppDelegates. Since, we added CoreData after our project initialisation and we donβt want to clutter the application delegate with the setup of the Core Data stack. we are going to create a separate class that is responsible for setting up and managing the Core Data stack.
Create a new Swift file in the Models
group and name the file CoreDataManager
. This is the class that will be in charge of the Core Data stack of the application.
We start by adding an import statement for the CoreData
framework and define a singleton CoreDataManager
class.
Now lets dig in AppDelegate.swift
file and discuss about code. The AppDelegate file contains application life cycle methods and code stubs related to core data.
Initialize NSPersistentContainer class and thus core data stack objects (Managed object Model, PersistentStoreCoordinator, Managed Object Context).
A method named save(). It saves managed object models in to store.
Application life cycle method named applicationWillTerminate calls saveContext() also to save data in store when app is about to terminate.
The below code creates a
NSPersistentContainer
object. An instance of NSPersistentContainer includes all objects needed to represent a functioning Core Data stack. NSPersistentContainer is a container that encapsulates the Core Data stack (Heart of the core data) in your application. NSPersistentContainer simplifies the creation/initialization and management of the Core Data stack by handling the creation of below objects:- β managed object model (NSManagedObjectModel), β persistent store coordinator (NSPersistentStoreCoordinator), β and the managed object context (NSManagedObjectContext).
Now add the following code in
didFinishLaunchingWithOptions
in AppDelegate to load our data store.
In the above code, persistentContainer = NSPersistentContainer(name: modelName)
Initializes a persistent container with the given name "MyNotes"
. Where parameters (name :"MyNotes") is the name of the NSPersistentContainer object and return a persistent container initialized with the given name.
When persistent container have been initialized, we need to execute load
to instruct the container to load the persistent stores and complete the creation of the Core Data stack. Once the completion handler has fired, the stack is fully initialized and is ready for use.
storeDescription β As the name of the class suggests, NSPersistentStoreDescription class encapsulates the information and configuration to add a persistent store to the persistent store coordinator. In other words, it describes a persistent store.
In above code we are calling persistent containerβs view context object. This view context is name of property of type ManagedObjectContext class. Function save
looks for the changes made on managed object model. If there is any change we call viewContext.save()
method of context object to finally save our data.
Letβs create a method createNote()
that will initialize a Note object. We canβt just simply initialize a new note as let note = Note()
, we need to initialize it with a context. And after initialization, we call save()
function to save the changes in our viewContext
.
Method fetchNotes()
will return an array of Notes. First, we create a request of type NSFetchRequest
and assigning equal to Note.fetchRequest()
which is a class method in Properties file.
To filter the contents of the Notes, we will use NSPredicate
Predicates are simple tests, which are used to filter out the data which you need in our resultant array of data. The test will be applied to every object in your Core Data entity. Next thing we want to do is sort which one will be most recently updated notes. We create a sortDescriptor of type NSSortDescriptor and add it to the request and in last return viewContext.fetch
Method deleteNote()
, we will pass a note object to viewContext.delete()
We can call these methods for CRUD operations according to our needs.
π‘ Core Data provides on-disk persistence, which means your data will be accessible even after terminating your app or shutting down your device. This is different from in-memory persistence, which will only save your data as long as your app is in memory, either in the foreground or in the background.