Introduction
In the first article we learnt about data model and created our xcdatamodeld
file which holds all our Core Data models in it. In this article we will explore what is NSPersistentContainer
and what role does it play in the Core Data stack.
NSPersistentContainer
NSPersistentContainer
is the class that is responsible for actually persisting and reading the data from the permanent data store. This class creates the data store which may be SQLite, some flat file system or whatever it can. But we use this class to help us save the data and retrieve it from the data store.
So we will use NSPersistentContainer
to load create the database and make sure everything work with data models we have created. We will create a new class AppPersistenceContainer
import Foundation
class AppPersistentContainer {
}
Create the object of NSPersistentContainer
and initialize it in init
of the class
import Foundation
import CoreData
class AppPersistenceContainer {
// Step - 1: Create the object
private var persistenceContainer: NSPersistentContainer
init() {
// Step - 2: Initialize the object
persistenceContainer = NSPersistentContainer(name: "CoreDataExampleModel")
}
}
If you look carefully in Step - 2
we are using the exact name of the xcdatamodeld
file in the initializer. Make sure it matches your xcdatamodeld
file name without the extension.
Not lets initialize the AppPersistenceContainer
class in CoreDataExampleApp.swift
file.
import SwiftUI
@main
struct CoreDataExampleApp: App {
// Add this line.
private var appPersistentContainer = AppPersistenceContainer()
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Now if everything is correct your app should build and run fine. You won't see anything much on the screen as we have not created on data and done anything to show data on the view. But the logs should not show any error.
But if you have named the data model incorrectly in NSPersistentContainer(name: String)
then you will see the following error in the logs. App won't crash or anything but things won't work as expected if try to load or save any data.
There is one more step we can do to make 100% sure that the persistent store has been loaded or if there is error we can handle it gracefully. Now after we have initialized NSPersistentContainer
in the init
of AppPersistenceContainer
add this line of code.
persistenceContainer.loadPersistentStores { description, error in
if let error {
print("❌ ERROR: Failed to load persistent store with error: \(error.localizedDescription)")
return
}
}
Your whole AppPersistentContainer.swift
class should look something like this:
import Foundation
import CoreData
class AppPersistenceContainer {
private var persistenceContainer: NSPersistentContainer
init() {
persistenceContainer = NSPersistentContainer(name: "CoreDataExampleMode")
persistenceContainer.loadPersistentStores { description, error in
if let error {
print("❌ ERROR: Failed to load persistent store with error: \(error.localizedDescription)")
return
}
}
}
}
The code that we added just now, whats its doing is that it is loading the Persistent store and if it cannot for some reason, the error will be handled in the loadPersistentStore
closure.
In the next article we learn more about NSManagedObjectContext
and how it is used to actually save objects of our FolderEntity
data model and save it.
Top comments (0)