Re: Dealing with child's editing contexts [explained] :-)
Re: Dealing with child's editing contexts [explained] :-)
- Subject: Re: Dealing with child's editing contexts [explained] :-)
- From: Lachlan Deck <email@hidden>
- Date: Wed, 30 Aug 2006 02:45:32 +1000
Hi Yann,
On 28/08/2006, at 12:35 AM, Yann Bizeul wrote:
This is Location's awakeFromInsertion method, it has a to-many
relationship with LocationTagUser and a to-one relationship with
Address.
What is the best way to include the whole package to
EOEditingContext ?
Step back a second... EOF handles this for you in most cases.
EOEnterpriseObject eo; // yet to be assigned...
An object is inserted into an ec in the following ways:
1) You create new object:
eo = EOUtilities.createAndInsertInstance( ec, "entityName" );
OR
eo = new EntityName();
ec.insertObject( eo );
After either of the above you are now ready to use the object. And
sure, awakeFromInsertion is the place for initialising default values
- but NOT testing if a related object is in the same ec. See below.
2) You fetch the object(s) from the database via an editing context:
Enumeration en;
en = ec.objectsWithFetchSpecification( fetchSpec ).objectEnumerator();
while ( en.hasMoreElements() ) {
eo = ( EOEnterpriseObject )en.nextElement();
}
3) You follow a relationship key/method:
eo = someAlreadyInsertedObject.valueForKey( "relationshipKey" );
// OR e.g.,
eo = ( ( EntityName )eo ).locationTagUser();
4) You copy an object from one editing context to another:
eo = EOUtilities.localInstanceOfObject( newEc, eo );
That's it.
So to answer your question...
Is this code WOSpirit complient ?
public void awakeFromInsertion(EOEditingContext ec)
{
super.awakeFromInsertion(ec);
setDate(new NSTimestamp());
if (address().editingContext() == null)
ec.insertObject(address());
int i=0;
for (i=0;i<locationTagUsers().count();i++)
{
LocationTagUser l = (LocationTagUser) locationTagUsers
().objectAtIndex(i);
if (l.editingContext() == null)
ec.insertObject(l);
}
}
No. If you're creating a new LocationTagUser or Address then you'll
do the following:
EOEditingContext ec;
LocationTagUser aUser;
ec = aLocation.editingContext();
aUser = ( LocationTagUser )EOUtilities.createAndInsertInstance( ec,
"LocationTagUser" );
aLocation.addObjectToBothSidesOfRelationshipWithKey( aUser,
"locationTagUsers" );
<... etc for any new objects ...>
ec.saveChanges();
See the points above...
Finally, let's say you fetched via ec1 objects of entity Location and
via ec2 objects of entity LocationTagUser. The objects fetched are
already related via a previous save to the database (via ec0) but
you've operated on these separately in differing contexts. These
changes won't be visible to the other context until a save/or
localInstanceOfObject is done. Note: you cannot get a local instance
of an uncommitted eo.
HTH...
with regards,
--
Lachlan Deck
_______________________________________________
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