Saturday, February 26, 2011

Core Data Introduction


Hello readers in this post we will learn what is core data .

But before learning this we must be very clear with the basic concepts of core data and that is what we will be seeing first.

What is Core Data and what is it used for?

Well in a few words if I would like to describe core data that would be it is used to store data from your iPhone application into a sqlite file which is present in the document directory of your application, now if you ask me what is document directory I would say it is nothing but a directory inside the application folder which is created to store the application oriented data such as some files, images, sqlite database etc. With the release of version 3.0, Apple has made their Core Data system available on the iPhone OS.

Core Data has been available on the Macintosh platform for a number of years, and provides a high-level data abstraction framework that offers integrated design tools and runtime support to address complex data management needs unlike SQLite, the Core Data model is not strictly relational in nature.

Ok now coming to the million dollar question and that’s

if core data is used to store data for our application then why do we use SQlite,

for those of you who don’t know what is SQlite well you can see SQlite is a relational database used for mobiles. Core Data has some significant advantages. Apple provides development tools that allow a developer to quickly lay out their data requirements and relationships this can reduce development time and save on code.

Core Data is not a relational database , you can see Core data as a wrapper around Sqlite although core data is quite simpler as compared to Sqlite but it does not offer some of the functionality that Sqlite can offer and vice versa.

In Core Data each and every entity is referred to as objects, objects which have relationship with them.Let’s say there are two entities Parent and Child having relationship with them then in this case Parent and Child will be referred as objects and in Core data and you can easily manipulate them. If your application is particularly well suited to the Relational Model, there may be advantages to having direct SQL query access to the data storage layer.


Some of the basic classes that you must know in order to proceed with core data are given below:



ManagedObject: Managed objects are the objects that are created by your application code to store data. A managed object can be thought of as a row or a record in a relational database table. For each new record to be added, a new managed object must be created to store the data. Similarly, retrieved data will be returned in the form of managed objects, one for each record matching the defined retrieval criteria. Managed objects are actually instances of the NSManagedObject class, or a subclass thereof. These objects are contained and maintained by the managed object context.

Persistance store coordinator: The persistent store coordinator is responsible for coordinating access to multiple persistent object stores. As an iPhone developer you will never directly interact with the persistence store coordinator and, in fact, will very rarely need to develop an application that requires more than one persistent object store. When multiple stores are required, the coordinator presents these stores to the upper layers of the Core Data stack as a single store.

Managed Object Context: Core Data based applications never interact directly with the persistent store. Instead, the application code interacts with the managed objects contained in the managed object context layer of the Core Data stack. The context maintains the status of the objects in relation to the underlying data store and manages the relationships between managed objects defined by the managed object model. All interactions with the underlying database are held temporarily in within the context until the context is instructed to save the changes, at which point the changes are passed down through the Core Data stack and written to the persistent store.

In my next post i will show you how to add data with the help of core data

i hope this post was helpful to you do write in your comments or you may request a tutorial 
happy iCoding and have a great Day.

5 comments:

  1. Hey Ravi
    How u doing I need some more tutorial and live project on Coredata.
    and Also some interview questions
    @shashank.kuls@gmail.com

    Thanks in advance give my regards to Bharat and Sonal

    ReplyDelete
  2. Hey hi shashank how are you,

    First try creating a simple add/update/delete program in code data and then go for using core data module in your app.

    Also for the interview question they are already present in my blog and a tip from me is make sure that your fundamentals on iOS is cleared also on core data and webservices as these are the two topics which are commonly asked.

    ReplyDelete
  3. When i searched for this, powercase had some great resolutions to it.

    ReplyDelete