How to Easily Save, Fetch, Update and Delete Data Using Swift with Core Data

20 Jul 2023 Balmiki Mandal 0 Swift Programming

How to Save, Fetch, Update and Delete Data from Core Data Using Swift

Core Data is a framework that allows developers to store and manage data on iOS devices. It is also used as a persistence layer in many apps. Core Data provides an object-oriented way of working with data, making it easier for developers to work with their data. In this article, we’ll explore how to use Core Data to save, fetch, update and delete data using Swift.

Setting up Core Data

Before we can start working with Core Data, we need to set up the Core Data stack. Setting up the core data stack involves creating a managed object model, creating a persistent store coordinator, and connecting it all together in a managed object context.

Saving data with Core Data

To save data using Core Data, we first need to create a managed object and set its properties. We then call the "save" method of the managed object context to persist the data in the persistent store. Here’s how we can save data using Core Data:

let entity = NSEntityDescription.entity(forEntityName: “MyEntity”, in: managedObjectContext)
let object = NSManagedObject(entity: entity, insertInto: managedObjectContext)

object.setValue("foo", forKey: "name")
try? managedObjectContext.save()

Fetching data with Core Data

Once we have saved some data, we can fetch it back using a fetch request. We can create a fetch request by providing the entity name and the sort descriptor. The fetch request can be executed using the “execute” method of the managed object context. Here’s an example of how to fetch data from Core Data:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "MyEntity")
let sortDescriptor = NSSortDescriptor(key: “name”, ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

do {
   let fetchedResults = try managedObjectContext.fetch(fetchRequest) as? [NSManagedObject]

   // do something with the fetched results
} catch {
   // handle the fetch error
}

Updating data with Core Data

We can update existing data using the "save" method of the managed object context. To update data, we first need to fetch the existing data, then make changes to it and finally call the “save” method. Here’s an example of how to update data with Core Data:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "MyEntity")
let sortDescriptor = NSSortDescriptor(key: “name”, ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

do {
   let fetchedResults = try managedObjectContext.fetch(fetchRequest) as? [NSManagedObject]

   let object = fetchedResults.first
   object.setValue("bar", forKey: "name")

   try? managedObjectContext.save()

} catch {
   // handle the fetch error
}

Deleting data with Core Data

Finally, we can delete data with Core Data using the "delete" method of the managed object context. To delete data, we first need to fetch the existing data and then call the “delete” method. Here’s an example of how to delete data with Core Data:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "MyEntity")
let sortDescriptor = NSSortDescriptor(key: “name”, ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

do {
   let fetchedResults = try managedObjectContext.fetch(fetchRequest) as? [NSManagedObject]

   let object = fetchedResults.first
   managedObjectContext.delete(object)

   try? managedObjectContext.save()

} catch {
   // handle the fetch error
}

Conclusion

In this article, we explored how to use Core Data to save, fetch, update and delete data using Swift. We began by setting up the Core Data stack, then moved on to saving, fetching, updating and deleting data. Finally, we discussed how to save, fetch, update and delete data using Core Data.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.