print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
→ The above code allows you to easily locate where your SQLite file is.
( 3 Entities )
→ If we put these three entities into permanent storage, that becomes our Persistent Container.
→ And this contained is simply a SQLite database that stores all of our tables and all of the relationships between the tables.
→ We have to go through a intermediary, which is called Context.
In Core Data, you do CRUD inside the "Context", not directly to the persistent container.
Once you've decided that you're happy with what you've done inside the context, do you save the context using " try context.save( ) " .
→ Then, Core Data takes care of the rest and saves what inside the context to the Persistent Container.
→ Very similar to Git.
In summary, in Core Data, we have a persistent contained and our app is NOT allowed to interact directly with a persistent container. Instead, it has to go through this temporary area which is known as the Context.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Explanation of the above code:
→ The first thing we create is a constant called "context".
→ This constant goes into the AppDelegate and grabs the persistentContainer.
→ And then we grab a reference to the "context (viewContent)" for that persistentContainer.
NSManagedObject
let newItem = Item(context: self.context)
newItem.title = textField.text!
newItem.isDone = false
→ Then we fill up the fields (attributes or properties).
Then, we save the data.
func saveItems() {
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
self.tableView.reloadData()
}