Re: Fetch Spec Misunderstanding
Re: Fetch Spec Misunderstanding
- Subject: Re: Fetch Spec Misunderstanding
- From: LD <email@hidden>
- Date: Thu, 28 Jul 2005 10:31:45 +1000
Hi there,
On 28/07/2005, at 9:21 AM, Darich Runyan/OMNI INFOSEC LTD HQ wrote:
Consider using the EOSharedEditingContext. That's what it's made for.
If you have the Apple WO documentation, you should find the
EOSharedEditingContext API with your browser at:
file:///Developer/Documentation/WebObjects/Reference/API/
index.html
Would this EOSharedEditingContext be instantiated in Application? The
documentation implies this where it says that they are "persist for
the life
of the application;" however, it does not actually specify if it is
within
the Application startup that they get built.
Setting up your EOSharedEditingContext in Application is the [only]
sensible place as the entities are shared by multi-sessions and
session-less requests.
Here's what I'm doing - which works well...
public Application() {
super();
<...>
sharedEditingContext =
EOSharedEditingContext.defaultSharedEditingContext();
System.out.println("sharedEditingContext is set...");
en = sharedEntities.objectEnumerator();
while (en.hasMoreElements()) {
String entityName;
entityName = (String) en.nextElement();
try {
refetchSharedRecordsForNamedEntity(entityName);
} catch (Exception e) {
System.err.println("Shared Fetch Error: " +
e.getMessage());
e.printStackTrace();
}
}
<...>
}
// this method is for regular use after entities are in the
shared EC.
/** @TypeInfo EOEnterpriseObject */
public NSArray sharedRecordsForNamedEntity(String entityName) {
return (NSArray) sharedEditingContext.objectsByEntityName
().valueForKey(entityName);
}
// Call this method when initialising the sharedEC for entity
named entityName.
// Also use it when you've inserted new objects for the entity
in another ec that's
// saved its changes to the datastore. It's not necessary to
call this when items
// have been updated (i.e., edited or deleted) as the sharedEC
will pick those changes up
// via notifications[1]
/** @TypeInfo EOEnterpriseObject */
public NSArray refetchSharedRecordsForNamedEntity(String
entityName) {
EOFetchSpecification fetchSpec;
NSArray records;
try {
// test for custom fetch spec.
// i.e., a static method in the entity's class called
FetchSpecification
Class aClass = Class.forName(entityName);
Method aMethod = aClass.getMethod("FetchSpecification",
null);
fetchSpec = (EOFetchSpecification) aMethod.invoke
(aClass, null);
} catch (Exception e) {
// otherwise just fetch by entity name
fetchSpec = new EOFetchSpecification(entityName, null,
null);
}
try {
// attempt to fetch the shared entities into the sharedEC
records =
sharedEditingContext.objectsWithFetchSpecification(fetchSpec,
sharedEditingContext);
} catch (RuntimeException rte) {
System.err.println("Refetch error for entity: " +
entityName + " " + rte.getMessage());
rte.printStackTrace(new PrintStream(new FileOutputStream
(FileDescriptor.err)));
records = NSArray.EmptyArray;
}
return records;
}
[1] inserting new items for an entity requires using an ec that's not
the shared ec and whose shared ec is null. i.e.,
editingEC = <init>;
editingEC.setSharedEditingContext(null);
newEO = EOUtilities.createAndInsertInstance(editingEC, entityName);
<...>
editingEC.saveChanges();
Application.application().refetchSharedRecordsForNamedEntity
(entityName);
with regards,
--
LD
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden