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, f开发者_运维技巧or one data inside the sqlite file?
Thanks for your answers
Paul
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 theNSAtomicStore
andNSIncrementalStore
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 theNSManagedObject
subclass that should be usedNSManagedObjectContext
- This is the in memory "scratch pad". It is where you query for objects (usingNSFetchRequests
), 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
精彩评论