Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core data : differences between managed object and entities?

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

like image 760
Paul Avatar asked Aug 01 '11 20:08

Paul


1 Answers

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

like image 101
Martin Pilkington Avatar answered Sep 30 '22 11:09

Martin Pilkington