What is CoreData?, How to use CoreData? Why to use CoreData?, these questions come in every junior iOS Developer. If you belong to the same context, you are on the right page. we will see the basics of CoreData in Swift.
Sometimes we have to handle an ample amount of local data in a sophisticated manner. Coredata is an object-oriented database framework. Everything in Coredata is represented as an object like Manage Objects.
CoreData framework allows user to store and retrieve persistent data in an application.
Coredata is not a Database:
Yes, Right CoreData is not a database, it’s just a framework that works as a mediator between SQLITE and Application. SQLite itself is a database.
Apple created Core-data framework to perform sensible operation in sqlite database in easy way.
Usually, the database is backed by SQL (Structure query language) but here in CoreData database is backed by the Object graph.
In terms of Data handling, Data customization, Data model design, The CoreData is very powerful as compared to having direct use of sqlite database syntax.
Coredata is very easy:
CoreData is very easy to understand and implement in Xcode.
Due to back-breaking syntax and toughness in the implementation of SQLite, Apple introduced Coredata in developer favor.
Need not to write queries for INSERT, UPDATE, DELETE operation like SQL (Structure query language). We can have access to model properties with dot(.) extension.
Example :- user.name = “Rahul”
Coredata follows object-oriented relation ethics:
CoreData allows abstraction to interact with model in in Object oriented manner.
CoreData not allows you to write queries to fetch and insert data in like any other query language does.
CoreData forms every table of sqlite in the form of object structure.
Coredata is M in MVC, it’s Model layer operation of Application, Creating Model is the primary use of CoreData, and storing persistent data is the optional use of CoreData.
If you want a lightweight data model, then don’t use CoreData, If you want to persist lightweight data like user sessions or any other key-value data, you can use UserDefaults or Plist rather than CoreData.
Coredata provides UI Interface:
Coredata give visual editor support to create entities(Tables), relationships and settings.
Coredata supports visual mapping and schema, means we can have a visual presentation of the database, tables, relationships, and other configuration.
Having UI interface in object graph manner and their relationships are a very useful feature to understand and optimize the structure of Database.
CoreData allows easy data migration in terms of data when APP build updated on AppStore.
Data migrations allows to safeguard users data after changes done in Data Model by creating new version of data model.
There are two types of data migration light weight Data migration and heavyweight data migration. Following are the examples of data migration.
Light weight Data migration : Add new column in table or entity
Heavyweight data migration : Edit name of earlier column name line, name to fullName.
Lightweight Data migration handled by CoreData itself. But heavyweight data migration developers should handle it explicitly.
Version management of data layer in CoreData is handled very effectively.
Data should in memory before perform any operation on data.
Example: Before deleting a thousand records from the database, we have to fetch this data from the database and then we can delete this data. But in the case of SQLite data operation, we can directly delete data by executing query without storing it into memory.