Re: Inserting/Deleting Records From Within EOGenericRecord
Re: Inserting/Deleting Records From Within EOGenericRecord
- Subject: Re: Inserting/Deleting Records From Within EOGenericRecord
- From: Jonathan Rochkind <email@hidden>
- Date: Tue, 9 Mar 2004 16:03:24 -0600
At 3:52 PM -0500 3/9/04, email@hidden wrote:
To be specific, Subscriber is the EOGenericRecord and it has a one
to many relationship with Subscription.
(Subscriber<-(toSubscriptions)->>Subscription)
When I delete a subscription from subscriber by doing the following
nothing happens:
this.removeFromToSubscriptions(subscription);
this.removeObjectFromBothSidesOfRelationshipWithKey(subscription,"toSubscriptions");
First of all, these two lines are not neccesary---the second one does
the same thing as the first (plus more). And it's possible that
doing the first one first will make the second one do nothing (that
is, not do it's "more"), because it's already been done. I'm having
trouble saying this clearly, but: Just use the second line there,
you can omit the first.
Secondly, removing an object(s) from a relationship(s) is not the
same as deleting it. If you want to delete it, you've got to
ec.deleteObject( someEO ). In some cases deleting an object will
also have the side effect of removing it from it's relationships, but
it usually doesn't hurt to do both manually (the side effect might
not happen until saveChanges(), but you might want it removed from
the relationship immediately).
and then I actually delete the subscription record by calling the
following generic method:
EOEditingContext ec = this.defaultEditingContext();
EODatabaseDataSource ds = new EODatabaseDataSource(ec,table);
WODisplayGroup genericDisplayGroup = new WODisplayGroup();
genericDisplayGroup.setDataSource(ds);
genericDisplayGroup.setObjectArray(new NSArray(record));
genericDisplayGroup.selectObject(record);
genericDisplayGroup.delete();
this.defaultEditingContext().saveChanges();
I wouldn't bother creating an EODatabaseDataSource just for the
purpose of removing objects from a relationship or deleting them.
The EODatabaseDataSource is the wrong path here; don't bother with it.
I think EOEditingContext.deleteObject() is the method you are looking for.
The above deletes the record from the database but when I print out
"toSubscriptions" again there is the critter (and it hasn't been
deleted from the "object graph"? or the relationship graph of the
EOGenericRecord Subscriber).
Well, right, back to the idea that removing an object from a
relationship is not the same as deleting it. Two different
operations. Deleting it often will automatically remove it from the
relationship too, but sometimes won't. It depends on the
relationships defined and the delete rules on them. Shouldn't hurt to
manually remove it from the relationship AND delete it (with
ec.deleteObject()).
No errors have popped up as of yet.
So I insert another subscription from within the same
Subscriber.java as follows :
EOEditingContext ec = this.editingContext();
Subscription subscription =
(Subscription)(EOClassDescription.classDescriptionForEntityName("Subscription")).createInstanceWithEditingContext(null,null);
subscription.setSubscriberId(this.subscriberId());
ec.insertObject(subscription);
ec.saveChanges();
Firstly am I right to use this editing context from the EOGenericRecord ?
Sure, if that's the editing context you want. Should be fine. Except
if you are using the createInstanceWithEditingContext method, why not
actually pass in the EC as an argument? Instead of passing in null as
an argument and then later inserting it in the EC. In general, it's
best to insert a new object in an EC _immediately_, before doing
anything else. createInstanceWithEditingContext will do that for you
if you pass in an EC argument. Or you can insert it manually, but I
reccomend you do it right away. In this case, it shouldn't hurt to
call setSubscriberID() before calling insertObject()----but it's best
practice not to anyway. Insert right away, before doing anything
else. Or pass in an EC argument, and createInstanceWithEditingContext
will do it for you.
You know createInstanceWithEditingContext isn't the only way to
create and insert a new EO though, right? It's kind of an unusual
way to do it in fact, but will work fine, nothing wrong with it.
As I insert another subscription from within the same subscriber it
inserts ANOTHER subscription into the same subscriber and moves on
until it hits the lines that ask it to save the changes :
ec.insertObject(subscription);
ec.saveChanges();
If subscription is a variable still pointing to the same Subscription
object----why are you inserting it again? You only need to call
insertObject() _once_ for a given EO. And you should never call it
more than once.
Hope this helps. You've got a bunch of different things going on, a
bunch of different not entirely related questions, and several things
you are doing that aren't neccesarily quite right.
--Jonathan
At this point it gives an error :
NSInvalidArgumentException: Exception while evaluating
takeValue:forKeyPath:'child1.grade', on target:: ***
-[NSConcreteMutableArray addObject:]: attempt to insert nil
Where child1 is another to-many relationship Subscriber.
What I don't understand is why a subscription record shows up in
toSubscription() when I expressly delete it.
A. Shiraz
44 Executive Boulevard
Elmsford, NY 10523
(914) 592- 4203 x268
_______________________________________________
WebObjects-dev mailing list
email@hidden
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.