Swift - Introduction to Core Data in iOS

Core Data is not a relational database like Oracle. It is actually a framework that let developers store (or retrieve) data in database in an object-oriented way. With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL. It's been around for more than a decade and it first made its appearance on macOS with the release of macOS Tiger in 2005. In 2009, the company made the framework available on iOS with the release of iOS3.

       It is true that there are other frameworks build by non-Apple engineers such as REALM which acts like Core Data. Core Data is the first and foremost a framework for managing an object graph.

Differ from SQLite

Core Data are often confused by the difference between SQLite and Core Data. If you wonder whether you need SQLite or Core Data, Remember that Core Data isn't a database.

SQLite:
  • 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.
Core Data: 
  • Don't have Data Constraints, if required need to implement by business logic.
  • Operates on in memory
  • Need to load entire data if we need to drop table or update
  • Fast in terms of record creation
Additionally apart from SQLite as back-end Cored data can use XML or binary format for storing data to disk.

Limitations of Core Data
    Apple Docs says: Core Data makes no guarantees regarding the security of persistent stores from untrusted sources(as opposed to store generated internally) and connect detect whether files have been maliciously modified. The SQLite store offers slightly better security than the XML and binary stores, but it should not be considered inherently secure. Note, also that it is possible for data archived in the metadata to be tampered with independently of the store data. To ensure data security, use a technology such as an encrypted disk image.

Exploring Core Data Stack
    Now that you are aware of what Core Data is, now its time to zoom in on the building blocks of the framework. It's essential that you understand how the various classes that make the framework play together
  • The Managed Object Model
  • The Managed Object Context
  • The Persistent Store Co-ordinator
Fig-illustrates the Core Data Stack


Managed Object Model
     The managed object model is an instance of the NSManagedObjectModel class. while a typical Core Data application has one instance of the NSManagedObjectModel class, it's possible to have multiple. The managed object model instance represents the data model of the Core Data application.
      The managed object model describes the schema that you use in the app. Schema is represented by a collection of objects(also known as entities). In Xcode, the Managed Object Model is defined in a file with an extension .xcdatamodelId.
        An entity can have attributes and relationships, which make up the data model of  the application. Remember for now that the managed object model an instance of the NSManagedObjectModel class and that it the data model of the Core Data application represents.

Managed Object Context
    Managed object context is represented by an instance of the NSManagedObjectContext class. A Core data application has one or more managed object contexts, each managing a collection of model objects, instances of the NSManagedObject class.
   The managed object context is the object you interact with most. It creates reads, updates and deletes model objects. From a developer's perspective, the NSManagedObjectContext class is the workhorse of the Core Data framework.

Persistent Store Coordinator
    A Persistent Store Coordinator associates persistent object stores and a managed object model, and presents a facade to managed object contexts such that a group of persistent stores appears a single aggregate store. A persistent store coordinator is an instance of NSPersistentStoreCoordinator. It has a reference to a managed object model that describes the entities in the store or stores it manages.
   The coordinator is the central object in a Core Data Stack. In many applications you just have a single store, but in complex applications there may be several, each potentially containing different entities. The persistent store coordinator's role is to manage these stores and present to it managed object contexts the facade of a single unified store. 


Thanks for reading...! I hope this would be helpful in understanding the concept, well, this is not enough, check my next post on how to implement core data in iOS using Swift

Comments