Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It provides a way to store data even after the existence of the application vanishes.
How Does It Differ From SQLite?
Developers that are new to programming are often confused about whether to use core data or SQLite.
Then you have to note that CoreData is not a database, it is just a framework.
These are the basic differences between core data and SQLite, given below.
- Have Data Constraints feature.
- Operates on data, stored on disk.
- Can Drop table and edit data without loading them in memory.
- Slow as compared to core data.
- Don’t have Data Constraints, if required, need to be implemented by business logic.
- Operates on in-memory. (data needs to be loaded from disk to memory)
- Need to load entire data if we need to drop tables or update.
- Fast in terms of record creation. (saving them may be time-consuming)
Core Data Limitations
There are several drawbacks. These drawbacks are directly related to the nature of the framework and how it works.
1: Core Data keeps the object graph in memory. It means that it can only operate on records once they’re in memory. This is very different from performing a SQL query on a database. If you want to delete thousands of records, Core Data first needs to load each record into memory.
2: Another important limitation is the threading model of Core Data. The framework expects to be run on a single thread.
For applications that need to manage a complex object graph, Core Data is a great fit. If you need to store only a handful of unrelated objects, then you may be better off with a lightweight solution or the user defaults system.
CRUD operation on CoreData
To understand, let’s create a simple demo app that shows us how to use core data in our Application.
1: Create a new iOS project and save it somewhere in the directory.
2: When you create a new project you see a new file named, coreDataDemoApp.xcdatamodel is added to your project.
3: When you click it, you will see a tool that allows you to configure entities which represent data models. You can add new entities, their attributes, and the relationship between the entities.
- Add a new user with the name user.
- Add some attributes to it.
4: When you open the app delegate.swift file you see some extra code with core data stack code.
Core Data Stack
The Core Data Stack code inside the AppDelegate.swift has clear documentation in form of comments but in short, it sets up the persistentContainer and saves the data if there are any changes. As AppDelegate is the first file that executes as soon as the app launches, we can save and fetch the context from the Core Data Stack.
Now, we have modelled our data in the User entity. It’s time to add some records and save it into the CoreData.
Start with importing CoreData to our viewController.swift
Create Records to Core Data
The process of adding the records to Core Data is followed by a few steps. First, you have to create a reference to the container present at appDelegate and create a context of that object.
After that, you have to create a new user entity with NSEntityDescription and add data to the user entity and lastly save the data back to the container.
You can see your saved data in the DBBrowser by just giving the path of your SQLite file and going to the browse data option.
For Getting the path of the SQLite file you have to just type the below statement to the debugger or in the print statement. It will give you the path where your file is saved just go to path and open it in DBBrowser.
For fetching the saved data, you have to Prepare the request of type NSFetchRequest for the entity. Fetch the result from context in the form of an array of [NSManagedObject. Iterate through an array to get value for the specific key
To update the record first we have to Prepare the request then Fetch the record and Set New value with the key And Last Save context same as creating data.
To delete records first we have to find the object which we want to delete by fetchRequest, Prepare the request and Fetch records which we want to delete. After this call context.delete (object).
CoreData is an object graph and persistence framework provided by Apple. CoreData is a fantastic way to save data locally within your system even after the existence of the application vanishes. The only drawback with CoreData is that it keeps object graphs in the memory which means if you want to perform any operation you have to load records into memory first.