core data crud in swift

Core Data is a powerful framework provided by Apple to persist data in iOS and macOS apps. Using Core Data, developers can perform CRUD (Create, Read, Update and Delete) operations on their app's data model. Here's a brief overview of the Core Data CRUD operations in Swift:

Creating Data

The first step to persisting data using Core Data is to create a data model. After designing the data model, developers should create instances of the model object and save them to the Core Data persistent store using a Managed Object Context.

main.swift
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Person", in: context)!
let person = NSManagedObject(entity: entity, insertInto: context)
person.setValue("John", forKey: "name")
person.setValue(30, forKey: "age")
do {
    try context.save()
} catch let error as NSError {
    print("Could not save. \(error), \(error.userInfo)")
}
460 chars
12 lines

Reading Data

After creating data, developers can read data from the Core Data persistent store using the fetch request.

main.swift
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
do {
    let people = try context.fetch(fetchRequest)
    for person in people {
        let name = person.value(forKey: "name") as? String ?? ""
        let age = person.value(forKey: "age") as? Int ?? 0
        print("Name: \(name), Age: \(age)")
    }
} catch let error as NSError {
    print("Could not fetch. \(error), \(error.userInfo)")
}
541 chars
14 lines

Updating Data

To update data, developers can use the Managed Object Context to retrieve data from the Core Data persistent store using the fetch request. Once the data is retrieved, developers can modify the properties and save the context to update the data.

main.swift
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
fetchRequest.predicate = NSPredicate(format: "name = %@", "John")
do {
    let people = try context.fetch(fetchRequest)
    let person = people[0]
    person.setValue(31, forKey: "age")
    try context.save()
} catch let error as NSError {
    print("Could not fetch. \(error), \(error.userInfo)")
}
495 chars
13 lines

Deleting Data

To delete data, developers can use the Managed Object Context to retrieve data from the Core Data persistent store using the fetch request. Once the data is retrieved, developers can delete the data by calling the delete() method on the Managed Object Context.

main.swift
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
fetchRequest.predicate = NSPredicate(format: "name = %@", "John")
do {
    let people = try context.fetch(fetchRequest)
    let person = people[0]
    context.delete(person)
    try context.save()
} catch let error as NSError {
    print("Could not fetch. \(error), \(error.userInfo)")
}
483 chars
13 lines

gistlibby LogSnag