Creating an Instance of EOGenericRecord

The best way to create an instance of EOGenericRecord is using the EOClassDescription method createInstanceWithEditingContext as follows:

EOEnterpriseObject newEO; String entityName; // Assume this exists. EOClassDescription description = ClassDescription.classDescriptionForEntityName(entityName); newEO = description.createInstanceWithEditingContext(null, null);

createInstanceWithEditingContext is preferable to using the constructor because the same code works if a custom enterprise object class is later used instead of EOGenericRecord. You can get an EOClassDescription for an entity name as shown above. Alternatively, you can get an EOClassDescription for a destination key of an existing enterprise object as follows:

  
EOEnterpriseObject newEO; EOEnterpriseObject existingEO; // Assume this exists. String relationshipName; // Assume this exists. EOClassDescription sourceDesc = existingEO.classDescription(); EOClassDescription desc = sourceDesc.classDescriptionForDestinationKey(relationshipName); newEO = desc.createInstanceWithEditingContext(null, null);

The technique in this example is useful for inserting a new destination object into an existing enterprise object for creating a new Movie object to add to a Studio's array of Movies, for example.