Core Data is an article chart and industriousness structure given by Apple in iOS and Mac OS.
Core Data is utilized to maintain the model layer object in our application. You can utilize Core Data as a system to track, save, change and filter the information inside iOS/Mac applications. However, Core Data isn’t a database. Core Data is utilizing SQLite as it’s a constant store however the core information system itself isn’t the database. Core Data does substantially more than databases like dealing with the article diagrams, following the changes in the information and a lot more activities.
In this article, we will perceive how to embed, update and erase information utilizing the Core Data system. With Core Data, you can undoubtedly map objects in your applications to the table records in the database without even information of any SQL.
Developers new to Core Data are confused by the differences between SQLite and Core Data. If you are confused about whether you need SQLite or Core Data, then you’re asking the wrong question. Remember that Core Data isn’t a database.
In order to understand the basics of Core Data, let’s create a single view iOS app and select the Core Data module.
We have created a demo project with Core Data Framework support. There are two notable changes in this Xcode template you can easily observe which are :
The Core Data Stack code is available inside the AppDelegate.swift file with clear documentation in the form of comments. In short, it set up the persistent container and saves the data if there are any changes. As you know AppDelegate is the first file that executes as soon as app is launched, we can save and fetch the context in the form of Core Data Stack.
The new CoreDataDemo.xcdatamodeld acts as the model layer for the data that we want to save. We can easily ad the entity, attributes and relationships from the UI as like any other relational database.
Suppose we want to store the username, email and birth_date attributes for the User entity. Select the CoreDataDemo.xcdatamodeld file and click on “Add Entity” (+) button and name the entity as “Users”. From the add username, email and birth_date as String type attributes. The end result will look like this.
Now we have modeled our data in the Users entity. Now it’s time to add some records and save it into the CoreData.
We need to follow below tasks in order to add data in Core Data.
First import CoreData to your ViewController.swift file.
Let’s start with Refer to persistentContainer which is already available in AppDelegate and Create Context:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
let newUser = NSManagedObject(entity: entity!, insertInto: context)
We need to add few data to our newly created records for each keys :
newUser.setValue("Viral", forKey: "username")
newUser.setValue("Viral123", forKey: "password")
Finally, we have set up all values, now save then inside the Core data. Already methods for saving the context exist in the AppDelegate.swift file but we can explicitly add this code to save the context in the Database. Note that, we have to wrap this with do try and catch block for handling the exception.
do {
try context.save()
} catch {
print("Failed while saving")
}
It’s also each to fetch saved data from Core Data. We need to follow the below tasks :
We need to fetch the data from our Users entity using the following code.
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
for data in result as! [NSManagedObject] {
print(data.value(forKey: "username") as! String)
}
} catch {
print("Failed")
}
Finally, you have fetched a username from CoreData for each record.
Our Swift developers are not only technically proficient but also have a strong commitment to continuous improvement. They stay updated with the latest industry trends, best practices, and emerging technologies to ensure that your project benefits from the most advanced and efficient approaches.
We have saved and retrieved data from Core data but these are basic operations to start with, we can do a lot more things with Core Data. We can modify, delete, track data changes, adding predicates. And it’s fun to do all these things with Core Data. Full Stack Developers are proficient in both front-end and back-end technologies to develop a complete web application from scratch.
If you face any technical difficulties feel free to contact our technical experts, we would love to resolve your queries. You can contact us here.
Stay tuned for the next post.
Using dynamic packaging technology, one can encapsulate flights, accommodation, cars,…
In today’s contemporary era of accelerated digital evolution, the importance…
The fact is that in today’s internet environment the only…
This website uses cookies.