Pages

Core Data

Overview of Core Data: 
    Core Data feels like a database but is not technically a database. It is a Object Graph that usually stores its data in SQLite database. It behaves like a database & can create, store & retrieve objects that have attributes & relationships to other objects. Basic advantage of Core Data is that it insulates the User from the underlying SQL, thats what SQLite basically called.

         Core Data uses many different classes to make its job done. These classes are divided into two groups, first group of classes is that set up once, when the App launches, these are:

NSPersistentStore

NSPersistentStoreCoordinator
NSManagedObjectModel


    
     First class NSPersistentStore represents a store, a place where our device's file system that stores the data. It's usually an SQLite database, but other like XML can be also.

     Second class NSPersistentStoreCoordinator represents the persistent store co-ordinator. The coordinator coordinates among persistent stores, object models, & object contexts. It uses the object model & tie it with one or more persistent stores. One more task it will perform is to coordinate the storage retrieval of objects between one or more objects contexts & persistent stores. Basically it is called as HUB, which ties together all Core Data Classes.

      Third class NSManagedObjectModel represents a managed object model, or you can call it a simple object model. Just like entity-relationship model it defines entities, attributes, & relationships. We can create a object model in our code, but Xcode provides an object model editor & it will let us create object model visually that ow application loads & uses it.

      Now second group of classes that we use throughout the runtime of our applications.

NSManagedObjectContext

NSManagedObject

     NSManagedObjectContext class represents an object context. Our application uses one or more of these object contexts, but usually uses one. These object contexts provides a place for objects to live, & basically titled as "scratch pad". With object context what you do is create the objects in the object context, retrieve the objects from the object context, & tell it to save itself. Object context is the one to take care of talking to persistent store coordinator to store & retrieve the objects.

     NSManagedObject class represents a managed object, or basically an object managed by Core Data. Usually you can think these as rows in database. Theses managed object have attributes & relationships as well. & it uses key - value coding so you can easily set & get the data of objects.

      Diagrammatically representation of these classes is shown below: 


Core Data Class Model




The diagram describes here that how all the classes related to each other in the Core Data Model.

After this overview I hope you get to know how the Core Data Model works. Now go ahead & see how to deal with Core Data programmatically in Xcode.




No comments:

Post a Comment