6

i would like to understand a bit more Core Data, why do we "fetch" and search for entities while the entities are "inside" managed objects? For example :

NSManagedObjectContext *moc = [self managedObjectContext];  
NSEntityDescription *entityDescription =
    [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];  
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];  
[request setEntity:entityDescription];

also, what does a persistent object store contain? if i understood, the persistent object store takes data from a sqlite file, but then it gets a bit confused, is it : one entity, for one persistent object store, for one data inside the sqlite file?

Thanks for your answers

Paul

Ryan
  • 16,626
  • 2
  • 23
  • 20
Paul
  • 6,108
  • 14
  • 72
  • 128

1 Answers1

16

Basically there are 5 components here. A persistent store coordinator, a managed object context, a managed object model, entities and managed objects. They all work together to provide an object graph management system (note that Core Data is not an ORM so it helps not to think of it that way). Below is a description of the components and the various other classes in CoreData that interact with them

  • NSPersistentStoreCoordinator - This handles the loading of data to and from disk. It deals with various stores (NSPersistentStore). The included store types are binary, XML and SQLite. You can write your own stores (using the NSAtomicStore and NSIncrementalStore classes), for example if you have your own file type (theoretically you could write a store to open a Word or Photoshop file if you desired)
  • NSEntityDescription - An entity can be sort of thought of as the "class" of a managed object. It defines any attributes (NSAttributeDescription), relationships (NSRelationshipDescription) and fetched properties (NSFetchedPropertyDescription) that a managed object should have, as well as other properties such as the NSManagedObject subclass that should be used
  • NSManagedObjectContext - This is the in memory "scratch pad". It is where you query for objects (using NSFetchRequests), create objects, delete objects etc. You can have multiple contexts, and throw one away without saving to discard any changes you no longer need.
  • NSManagedObject - The core unit of Core Data. These are your model objects that hold your data. You set the attributes, relationships etc on them.
  • NSManagedObjectModel - This represent the data model to use for your data, which is usually defined in a .mom file created within Xcode. This is where all the entities are stored.

That's pretty much the whole of core data. There are some other classes for doing migrations and merging

PeyloW
  • 36,742
  • 12
  • 80
  • 99
Martin Pilkington
  • 3,261
  • 22
  • 16
  • Like the answer but a few small changes. `NSManagedObjectModel` and the `NS*Description` classes all define what Core Data model should/can contain. `NSPersistentStoreCoordinator` is the class capable of realizing what could be in the model into what is in memory and vice versa. The `NSManagedObject` instances are the model realized in memory. The `NSManagedObjectContext` is the manager that ensures that what should be is eventually consistent with what is. – PeyloW Aug 01 '11 at 22:07
  • Thanks for both of your answers, i am still a bit confused with NSManagedObject and Entity : so the managedObject contains 1 entity, and the managedObjectModel contains all the managedObjects? or are they separated? – Paul Aug 02 '11 at 10:43
  • 2
    A managed object model contains all the entities. A managed object context contains all the managed objects. Every managed object has an entity (much like how every object has a class). – Martin Pilkington Aug 02 '11 at 14:17
  • alright... is the entity like a subclass of managed object? it just confuses me that we don't search for "managed objects" but for "entities", if as you said every manage object contains 1 entity – Paul Aug 02 '11 at 15:00
  • 3
    No. A managed object is like an instance of an entity. If you've done database work before imagine it like this, an entity is a table and a managed object is a row. You specify an entity to filter the managed objects. In the example you gave you are getting back all the managed objects of the entity Employee. Again, if you've done database stuff before, then in terms of SQL you might have done `SELECT * FROM Employee` – Martin Pilkington Aug 03 '11 at 20:53
  • Okay, thanks a lot! can i ask you a last question? the persistent object store contains one "row" from the sqlite file, and it will fill therefore one "managed object" (= one row of the entity) ? or does the persistent object store contain much more data? - so if you have 10 rows in your sqlite file representing 10 different datas, you'll have 10 persistent object stores, that will fill 10 managed objects in the entity? Thanks again – Paul Aug 04 '11 at 17:47
  • Roughly, persistent stores map to files on disk. So if you have a sqlite file that's a persistent store, if you have an xml file that's a persistent store etc. If you're wanting a more detailed overview I recommend the Pragmatic Programmer's Core Data book – Martin Pilkington Aug 04 '11 at 20:05
  • Thanks a lot, you've been a great help for me! – Paul Aug 05 '11 at 20:10
  • Adding to Martin Pilkington's answer with respect to Paul's question: Paul, it's the other way round; A managed object is subclass of an entity description. – Krishna Sapkota Apr 12 '14 at 11:04
  • 1
    @KrishnaSapkota That's not correct. A managed object is created using an entity description, but is not a subclass of it (all managed objects are subclasses of NSManagedObject which in turn is a subclass of NSObject). – Martin Pilkington Apr 17 '14 at 15:24