Core Data
Encyclopedia
Core Data is part of the Cocoa
Cocoa (API)
Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...

 API in Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

 first introduced with Mac OS X 10.4 Tiger
Mac OS X v10.4
Mac OS X v10.4 Tiger is the fifth major release of Mac OS X, Apple's desktop and server operating system for Macintosh computers. Tiger was released to the public on 29 April 2005 for US$129.95 as the successor to Mac OS X Panther , which had been released 18 months earlier...

 and for iOS with iPhone SDK 3.0. It allows data organised by the relational entity-attribute model
Entity-Attribute-Value model
Entity–attribute–value model is a data model to describe entities where the number of attributes that can be used to describe them is potentially vast, but the number that will actually apply to a given entity is relatively modest. In mathematics, this model is known as a sparse matrix...

 to be serialised
Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...

 into XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

, binary
Binary file
A binary file is a computer file which may contain any type of data, encoded in binary form for computer storage and processing purposes; for example, computer document files containing formatted text...

, or SQLite
SQLite
SQLite is an ACID-compliant embedded relational database management system contained in a relatively small C programming library. The source code for SQLite is in the public domain and implements most of the SQL standard...

 stores. The data can be manipulated using higher level objects representing entities and their relationships. Core Data manages the serialised version, providing object lifecycle
Object lifetime
In computer science, the object lifetime of an object in object-oriented programming is the time between an object's creation till the object is no longer used, and is destructed or freed.In object-oriented programming , the meaning of creating objects is far more subtle than simple...

 and object graph
Object graph
An Object graph is a view of an object system at a particular point in time. Whereas a normal data model such as a UML Class diagram details the relationships between classes, the object graph relates their instances. Object diagrams are subsets of the overall object graph.Object-oriented...

 management, including persistence
Persistence (computer science)
Persistence in computer science refers to the characteristic of state that outlives the process that created it. Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown....

. Core Data interfaces directly with SQLite
SQLite
SQLite is an ACID-compliant embedded relational database management system contained in a relatively small C programming library. The source code for SQLite is in the public domain and implements most of the SQL standard...

, insulating the developer from the underlying SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....

.

Just as Cocoa Bindings handles many of the duties of the Controller in a Model-View-Controller
Model-view-controller
Model–view–controller is a software architecture, currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" from the user interface , permitting independent development, testing and maintenance of each .Model View Controller...

 design, Core Data handles many of the duties of the data Model. Among other tasks, it handles change management, serializing to disk, memory footprint minimization, and queries against the data.

Usage

Core Data describes data with a high level data model expressed in terms of entities and their relationships plus fetch requests that retrieve entities meeting specific criteria. Code can retrieve and manipulate this data on a purely object level without having to worry about the details of storage and retrieval. The controller objects available in Interface Builder
Interface Builder
Interface Builder is a software development application for Apple's Mac OS X operating system. It is part of Xcode , the Apple Developer Connection developer's toolset. Interface Builder allows Cocoa and Carbon developers to create interfaces for applications using a graphical user...

 can retrieve and manipulate these entities directly. When combined with Cocoa bindings the UI can display many components of the data model without needing background code.

For example: a developer might be writing a program to handle vCard
VCard
vCard is a file format standard for electronic business cards. vCards are often attached to e-mail messages, but can be exchanged in other ways, such as on the World Wide Web or Instant Messaging...

s. In order to manage these, the author intends to read the vCards into objects, and then store them in a single larger XML file. Using Core Data the developer would drag their schema
Database schema
A database schema of a database system is its structure described in a formal language supported by the database management system and refers to the organization of data to create a blueprint of how a database will be constructed...

 from the data designer in Xcode
Xcode
Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS. Xcode 4.2, the latest major version, is available on the Mac App Store for free for Mac OS X 10.7 , and on the Apple Developer Connection website for free to registered developers Xcode is a suite of tools,...

 into an interface builder window to create a GUI for their schema. They could then write standard Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

 code to read vCard files and put the data into Core Data managed entities. From that point on the author's code manipulates these Core Data objects, rather than the underlying vCards. Connecting the Save menu item to the appropriate method in the controller object will direct the controller to examine the object stack, determine which objects are dirty, and then re-write a Core Data document file with these changes.

Core Data is organized into a large hierarchy of classes, though interaction is only prevalent with a small set of them.
Name Use Key Methods
NSManagedObject
  • Access attributes
  • A "row" of data
  • -entity
  • -valueForKey:
  • -setValue: forKey:
  • NSManagedObjectContext
  • Actions
  • Changes
  • -executeFetchRequest: error:
  • -save
  • NSManagedObjectModel
  • Structure
  • Storage
  • -entities
  • -fetchRequestTemplateForName:
  • -setFetchRequestTemplate: forName:
  • NSFetchRequest
  • Request data
  • -setEntity:
  • -setPredicate:
  • -setFetchBatchSize:
  • NSPersistentStoreCoordinator
  • Mediator
  • Persisting the data
  • -addPersistentStoreWithType: configuration: URL: options: error:
  • -persistentStoreForURL:
  • NSPredicate
  • Specify query
  • +predicateWithFormat:
  • -evaluateWithObject:


  • Storage formats

    Core Data can serialize objects into XML, Binary, or SQLite
    SQLite
    SQLite is an ACID-compliant embedded relational database management system contained in a relatively small C programming library. The source code for SQLite is in the public domain and implements most of the SQL standard...

     for storage. With the release of Mac OS X 10.5 Leopard
    Mac OS X v10.5
    Mac OS X Leopard is the sixth major release of Mac OS X, Apple's desktop and server operating system for Macintosh computers. Leopard was released on 26 October 2007 as the successor of Tiger , and is available in two variants: a desktop version suitable for personal computers, and a...

    , developers can also create their own custom atomic store types. Each method carries advantages and disadvantages, such as being human readable (XML) or more memory efficient (SQLite). This portion of Core Data is similar to the original Enterprise Objects Framework
    Enterprise Objects Framework
    The Enterprise Objects Framework was introduced by NeXT in 1994 as a pioneering object-relational mapping product for its NeXTSTEP and OpenStep development platforms. The EOF abstracts the process of interacting with a relational database, mapping database rows to Java or Objective-C objects. This...

     (EOF) system, in that one can write fairly sophisticated queries. Unlike EOF, it is not possible to write your own SQL.

    Core Data schemas are standarized. If you have the Xcode Data Model file, you can read and write files in that format freely. Unlike EOF, though, Core Data is not currently designed for multiuser or simultaneous access. Schema migration is also non-trivial, virtually always requiring code. If other developers have access to and depend upon your data model, you may need to provide version translation code in addition to a new data model if your schema changes.

    History and genesis

    Core Data owes much of its design to an early NeXT
    NeXT
    Next, Inc. was an American computer company headquartered in Redwood City, California, that developed and manufactured a series of computer workstations intended for the higher education and business markets...

     product, Enterprise Objects Framework
    Enterprise Objects Framework
    The Enterprise Objects Framework was introduced by NeXT in 1994 as a pioneering object-relational mapping product for its NeXTSTEP and OpenStep development platforms. The EOF abstracts the process of interacting with a relational database, mapping database rows to Java or Objective-C objects. This...

     (EOF).

    EOF was specifically aimed at object-relational mapping
    Object-relational mapping
    Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...

     for high-end SQL database engines such as Microsoft SQL Server
    Microsoft SQL Server
    Microsoft SQL Server is a relational database server, developed by Microsoft: It is a software product whose primary function is to store and retrieve data as requested by other software applications, be it those on the same computer or those running on another computer across a network...

     and Oracle
    Oracle database
    The Oracle Database is an object-relational database management system produced and marketed by Oracle Corporation....

    . EOF's purpose was twofold, one to connect to the database engine and hide the implementation details, and two to read the data out of the simple relational format and translate that into a set of objects. Developers typically interacted with the objects only, dramatically simplifying development of complex programs for the cost of some "setup". The EOF object model was deliberately set up to make the resulting programs "document like", in that the user could edit the data locally in memory, and then write out all changes with a single Save command.

    Throughout its history EOF "contained" a number of bits of extremely useful code that were not otherwise available under NeXTSTEP
    NEXTSTEP
    NeXTSTEP was the object-oriented, multitasking operating system developed by NeXT Computer to run on its range of proprietary workstation computers, such as the NeXTcube...

    /OpenStep
    OpenStep
    OpenStep was an object-oriented application programming interface specification for an object-oriented operating system that used a non-NeXTSTEP operating system as its core, principally developed by NeXT with Sun Microsystems. OPENSTEP was a specific implementation of the OpenStep API developed...

    . For instance, EOF required the ability to track which objects were "dirty" so the system could later write them out, and this was presented to the developer not only as a document-like system, but also in the form of an unlimited Undo command stack. Many developers complained that this state management code was far too useful to be isolated in EOF, and it was moved into the Cocoa API
    Cocoa (API)
    Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...

     during the transition to Mac OS X
    Mac OS X
    Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

    .

    Oddly what was not translated was EOF itself. EOF was used primarily along with another OpenStep-era product, WebObjects
    WebObjects
    WebObjects was a Java web application server from Apple Inc., and a web application framework that ran on the server. It was available at no additional cost. Its hallmark features were its object-orientation, database connectivity, and prototyping tools...

    , an application server
    Application server
    An application server is a software framework that provides an environment in which applications can run, no matter what the applications are or what they do...

     originally based on Objective-C
    Objective-C
    Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

     that was in the process of being ported to the Java programming language
    Java (programming language)
    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

    . As part of this conversion EOF was also converted to Java, and thus became much more difficult to use from Cocoa. Enough developers complained about this that Apple apparently decided to do something about it.

    One critical realization is that the object state management system in EOF did not really have anything to do with relational databases. The same code could be, and was, used by developers to manage graphs of other objects as well. In this role the really useful parts of EOF were those that automatically built the object sets from the raw data, and then tracked them. It is this concept, and perhaps code, that forms the basis of Core Data.

    External links

    The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
     
    x
    OK