16

What is difference between these three classes [NSManagedObject , NSManagedObjectContext , NSManagedObjectModel ] of core-data and how can we describe in easiest Way?

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
iMash
  • 1,178
  • 1
  • 12
  • 33

5 Answers5

52

In database terms:

  • NSManagedObject = a row
  • NSManagedObjectContext = a transaction
  • NSManagedObjectModel = a schema

Of course, Core Data isn't a database.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • 1
    Thanks. Its Short but Great answer. This can be the simplest way we can understand core data if we already worked on sql. Please provide some more details or any article link where such type of description and we can better know in terms of SQL. – iMash May 11 '12 at 13:04
  • 3
    A context is more like a client or connection than like a transaction. – paulmelnikow May 17 '12 at 02:57
  • A `NSManagedObjectContext` class, wraps a mutable layer around an `NSManagedObjectModel` instance. – Abhishek Bedi Feb 05 '13 at 09:51
  • 1
    @DeepakThakur sqlite isn't the only backend for Core Data. You can also use binary, XML, in-memory or even write your own (just saw one using [Realm](https://github.com/eure/RealmIncrementalStore/tree/master/RealmIncrementalStore)). Having said that, what they mean is, if you use it in the same way that you would use a SQL database, you're likely to have problems. And that's true, regardless of the underlying technology. – Stephen Darlington Feb 26 '16 at 10:06
6

From CoreData programming guide :

You can think of a managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered.

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
2

NSManagedObjects represent data stored in the database. You can think of them as model objects.

NSManagedObjectContext allows you to insert, save, and retrieve (using NSFetchRequest) NSManagedObjects from the database.”

Farooque Azam
  • 188
  • 1
  • 10
1

An NSManagedObject herits from object, and adds the methods necessary to be managed by core data.

The context is where the objects that are being managed by core data are saved, that happens when you take an object from the dataBase or you create to save to it

The object model describes a coredata schema for the persistent store

Jpellat
  • 907
  • 7
  • 29
0

context is u may say places where your objects resides after you read it or before you insert it into db

for more, read these threads breifly and understand the differenc

Add an instance of NSManagedObject to NSManagedObjectContext ok, updating the same instance failed

How do I copy or move an NSManagedObject from one context to another?

http://www.cimgf.com/2011/01/07/passing-around-a-nsmanagedobjectcontext-on-the-iphone/

Does an NSManagedObject retain its NSManagedObjectContext?

Community
  • 1
  • 1
Saad
  • 8,857
  • 2
  • 41
  • 51