A Guide to Understanding Core Data iOS
Core Data
Core Data is Apple’s persistence framework, which is used to persist—store our application’s data in a persistent store, which may be memory or a flat file database. It helps us represent our data model in terms of an object graph, establish relationships among objects, and it can also store object graphs on the disk. It also allows us to use the entities of our data model in the form of objects, that is, it maps our data into a form that can be easily stored in a database, such as SQLite, or into a flat file. Also, the Core Data reduces a lot of coding. On using Xcode’s templates for Core Data applications, we automatically get the boilerplate code that does several complex tasks such as generating XML files, binary files, SQLite files automatically for us without writing a single code, allowing us to focus on the business logic of our application.
Core Data features
The Core Data framework provides lots of features that include the following:
- Supports migrating and versioning: It means we can modify our data model , that is , entities of the application , whenever desired. The Core Data will replace the older persistent store with the revised data model.
- Supports Key-Value Coding(KVC): It is used to store and retrieve data from the managed objects
- Efficient database retrievals: Core Data queries are optimized for this , though the execution of query is dependent on the data store .
- Multi-threading:Core Data supports multi-threading in an application , more than one thread can be executed in parallel to increase performance.
- Inverse relationship: Core Data maintains an inverse relationship for consistency .
- External data repositories: Core Data supports storing objects in external data repositories in different formats
Data Model
Entities:
- Core Data maintains data in terms of objects , an entity is an individual data object to represent complete information of the person , item , object ,and so on.
Properties
- Properties of an entity give detailed information about it , such as what are its attributes and how it is related to other entities.A property of an entity refers to its attributes and relationships.
Attributes
- Attributes are the variables within and object(entity) . In fact , a collection of attributes makes an entity.In database language , they are known as columns of the table.
Relationships
- Besides attributes an entity may also contain relationships(which define how an entity is related to other entities). The attributes and relationships of an entity are collectively known as properties.The relationships are of many types(To-One, To-Many, and Many-to-Many) and play a major role in defining connection among the entities and what will be the impact of insertion or deletion of a row in one entity on the connected entities.
Core Data API
The Core Data API, also called the stack, consists of three main components:
- NSPersistentStoreCoordinator
- NSManagedObjectModel
- NSManagedObjectContext
Informative share to know about iOS...