EO duplication [was: Dealing with temporary EOs]
EO duplication [was: Dealing with temporary EOs]
- Subject: EO duplication [was: Dealing with temporary EOs]
- From: Florijan Stamenkovic <email@hidden>
- Date: Sun, 24 Jul 2005 17:42:15 +0200
I need to create a bunch of temporary EOs (dozens to hundreds, maybe
eventually a few thousand), which I then do a bunch of analysis on,
and present them to the user. The user may then save some of them to
the DB, or just ignore them all (this is the common case), in which
case they will be thrown away.
My initial attempt was to create a peer editing context, create all of
the EOs in that editing context (and get local instances of any EOs
from the main editing context for relationships), then if the user
elects to save any of them, do localInstanceOfObject(mainEC, object)
and then mainEC.saveChanges(). However, as I discovered from the list
archives, newly created EOs in peer editing contexts return null when
using localInstanceOfObject.
Hi Logan.
If you still after Art's advice are not willing for whatever reason to
delete objects from your peer context (although it is easier, nicer and
probably will perform faster), you might be able to implement the
following (probably sloppy) solution that is however proving to work in
my case:
Record oldrecord; //one in the old editing context you don't want to
save
EOEditingContext willSaveEc = new EOEditingContext() // ec only for
those records to be saved
Record newrecord =
(Record)EOUtilities.createAndInsertInstance(ec,"Record");
//set it's values
NSArray allKeys = oldrecord.allPropertyKeys();
int count = allKeys.count();
for(int i = 0 ; i < count ; i++)
{
String key = (String)allKeys.objectAtIndex(i);
newrecord.takeStoredValueForKey( oldrecord.storedValueForKey(key),
key);
}
This will effectively duplicate the record. Be careful with the
relationships however. Duplication of all keys will include
relationships, so you need to re-set the relationships to resolve to
objects in the willSaveEc:
EOGenericRecord object = newrecord.relationship();
newrecord.setRelationship(
EOUtilities.localInstanceOfObject(willSaveEc, object);
I use this in a different context where I have EOs with many attributes
of which very few are toOne relationships and none are toMany, and I
need to duplicate them... It does not support toMany relationships
(better to say, I have not tested it with toMany relationships), but
you could extend it and easily encapsulate to do so in a method called
something like duplicateObject()... that will then be able to duplicate
anything that subclasses EOCustomObject.
Have fun :P
Flor
_______________________________________________
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