PATH |
Fetching with an Editing Context
Discussion
Instead of using a display group to fetch objects, you can fetch enterprise objects directly with an EOEditingContext. Fetching from an EOEditingContext gains you more control over how the objects are fetched and enables you to directly manipulate the object graph. To fetch objects with an editing context involves these steps:
For WebObjects applications, each session has its own editing context which is obtained by sending the defaultEditingContext message to the session:
anEditingContext = [[self session] defaultEditingContext];
Otherwise you can simply create an editing context.
anEditingContext = [EOEditingContext new];
The fetch specification is an EOFetchSpecification object that tells EOF which objects you want to fetch and the order they should appear. See the programming topic "Creating Fetch Specifications Programmatically" to find out how to create the fetch specification. An example is given below.
aQualifier = [EOQualifier qualifierWithQualifierFormat: @"lastName = 'Smith'"];aFetchSpecification = [EOFetchSpecification fetchSpecificationWithEntityName: @"Employee" qualifier: aQualifier sortOrderings:nil];
You use the method objectsWithFetchSpecification which returns an array of enterprise objects that meets the fetch specification you have defined.
NSArray * results = [anEditingContext objectsWithFetchSpecification: aFetchSpecification];
EOUtilities Convenience API
The EOUtilities Application Program Interface (API) is a collection of convenience methods to simplify common operations with EOF. It is implemented as a category on EOEditingContext in Objective-C and as an abstract class in Java. To perform the fetch above using EOUtilities use:
NSArray *Smiths = [[[self session] defaultEditingContext] objectsMatchingValue:@"Smith" forKey:@"lastName" entityNamed:@"Employee"];
See Also
- Creating Fetch Specifications Programmatically
- Setting a WODisplayGroup's Fetch Specification Programmatically
- EOEditingContext class specification in the Enterprise Object Framework Reference
© 1999 Apple Computer, Inc.