Core Data (CRUD) with Swift for Beginners
Persist or cache data on a single device, or sync data to multiple devices with CloudKit.
Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organised by the relational entity–attribute model to be serialised into XML, binary, or SQLite stores.
Remember that Core Data isn’t a database, it’s a framework.
Benefits
- Don’t have Data Constraints, if required need to implement by business logic.
- Operates on in memory.(data needs to be loaded from disk to memory)
- Fast in terms of record creation.(saving them may be time consuming)
- Core Data includes mechanisms for versioning your data model and migrating user data as your app evolves.
- Additionally apart from SQLite as back-end Core data can use XML or binary format for storing data to disk.
Limitation
- Need to load entire data if we need to drop table or update: If you want to delete thousands of records, Core Data first needs to load each record into memory. It goes without saying that this results in memory and performance issues if done incorrectly.
Core Data Demo
Create single view iOS app with Core Data module selected.
If you want to host data on cloud then you can select “Host in CloudKit”. Also you can add this framework for your existing project.
Once the project is created, you will see two notable changes in this Xcode template.
- The new file CoreDataDemo.xcdatamodeld
- The AppDelegate.swift file with Core Data Stack code
Configure CoreDataDemo.xcdatamodeld File
Open CoreDataDemo file and Add new Entity.
Finally it’s look like this.
Core Data Stack
The Core Data Stack code inside the AppDelegate.swift is set up the persistentContainer and save the data if there are any changes. As AppDelegate is the first file that executes as soon as app launched, we can save and fetch the context the from the Core Data Stack.
Now that, we have modelled our data in the User entity. It’s time to add some records and save it into the CoreData
Create Records to Core Data
The process of adding the records to Core Data has following tasks
- Refer to persistentContainer from AppDelegate.
- Create the context from persistentContainer.
- Create an entity with User.
- Create new record with this User Entity.
- Set values for the records for each key.
Retrieve Data
The process of fetching the data is very easy as well. It has the following task
- Prepare the request of type NSFetchRequest for the entity ( here User entity).
- To filter the result use predicate.
- Fetch the result from context in the form of array of [NSManagedObject].
We can fetch the data from our User entity using following code.
Update Data
For update record first we have to fetch/Retrieve data with predicate, same as above Retrieve data process. Then below few steps to follow
- Prepare the request with predicate for the entity (here User entity).
- Fetch record and Set New value with key.
- And Last Save context same as create data.
Delete Data
For delete record first we have to find object which we want to delete by fetchRequest, then follow below few steps to delete the record
- Prepare the request with predicate for the entity (here User entity).
- Fetch record using predicate which we want to delete.
- Make context.delete(object) call.
Well, this isn’t enough to core data, there are many complex things we can do with core data tracking data changes, adding Predicates and complex relationships of databases. As you use more Core Data things get more complex.
Thanks 🙏🙏 Happy coding !!!!!!!
Follow me: https://www.linkedin.com/in/manvendra-singh-rathore-779003a8