IFeatureClass.CreateFeature() creates directly a feature but why?

2341
4
Jump to solution
04-03-2012 06:42 AM
SebastianKrings
Occasional Contributor
Hello,

I want to create a feature, so I do
IFeature newFeature = analyseFeatureClass.CreateFeature();


when reading this page:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/CreateFeature_Method/002...
explicitiy this sentence: Use the IFeature::Store method to actually store this new feature in the database.
and
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000049v000000
Store the feature???The feature is not written to the database until the IFeature.Store method has been called.
I would expect that I will get an IFeature object back from calling this method.
I also expect that this feature is not stored in my featureClass (at runtime)/ Geodatabase(after application closes) until I call IFeature.Store().

I debugged my program. Here's the log:
//before calling createFeature: 141 features in FeatureClass //Calling IFeatureClass.CreateFeature() //after calling createFeature: 142 features in FeatureClass


So after this single line the feature is still stored. Also when I directyl abort the whole program after this. There's no Feature.Store Method at all in my code.
I took the way to store my features by using an insertCursor, casting my created feature to a featureBuffer.
Due to this every feature is stored twice in my database.
the first one of each pair (through createFeature-method) does not contain any values, so it has null values (like documentation says, but wouthout the fact its really stored directly)
the second one contains the values I defined but is then stored as own feature getting its own OID.

So I guess that the calling createFeature method stored the feature directly to the featureClass but not to the database. When calling featureCurso.flush is called all featurebuffer plus all created features are stored then. If this is right I think the createFeature method does something like adding the feature to the featureBuffer query like using the featureCursor.insert(featureBuffer) method.

I think the simpliest solution not to make much changes to my code is to make use of IFeatureClass.CreateFeatureBuffer instead of createFeature. But if all what I am saying is right, then where's the difference of using createFeature followed by FeatureCurso.Flush() and using the correct Featurebuffer? And why the hell does ESRI documentation not offer or prevent developers from using this way of life?

Thanks for any useful answer!
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
It's not a bug; it is the way it has always worked.  The CreateFeature method creates the feature in the feature class immediately and returns the Feature object to you.  Any changes to the Feature object that you make will not be written to the feature class until you call the Store method.  So, if you call CreateFeature and set the Shape and other attributes but do not call Store then the Feature object in the feature class will have no geometry and the attributes will remain Null.  The CreateFeatureBuffer method returns a buffer to you.  This buffer is just an in-memory representation of a Feature.  No changes will be made to the feature class until you call the InsertFeature method on the feature cursor.  The documentation is not very clear on any of this.

View solution in original post

0 Kudos
4 Replies
AlexanderGray
Occasional Contributor III
Which object you use depends on what you are doing and how.  The createfeature assigns the ObjectID of your feature right away and the createdfeaturebuffer does not get a ObjectId until the you insertFeature.  What goes on in the database depends on the type of database and whether you have versioning.  Store will commit any changes to the feature instance to the database.  I would use createfeature and store but not with an insertcursor.  I would only use createfeaturebufferm insertFeature and flush with an insert cursor to do a batch job.  The flush as far as I can tell is only used for insert cursors.  It commits all the changes at once.  It is useless with the store method.  What gets me is why do insertcursors, updatecursors and searchcursors all implement the same interfaces.  You specifically have to handle them differently.
0 Kudos
SebastianKrings
Occasional Contributor
hi

I well know not to use createFeature with an insertCursor and vice versa. To solve the problem in the code I still changed createFeature to CreateFeatureBuffer and the code works fine.
But the problem what I have is to find out how they work together and why this all is possible and why documentation does not tell that some crossing code can manupulate each other.

I tested yesterday for hours and now I can exclude the FeatureCursor.
I made a small test project, loading an empty featureClass in my non-versioned FileGeodatabase and called FeatureClass.CreateFeature 10 times. Then the code stops. No Edit Session. No Cursors. No store-method.
After that the featureClass contains 10 empty features.

So ESRI is wrong in saying:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000049v000000
Store the feature�??The feature is not written to the database until the IFeature.Store method has been called.


This sounds like a bug, what do you think?
Maybe someone can check this on his/her system.
I have ArcDesktop 10 Editor with SP3.
0 Kudos
NeilClemmons
Regular Contributor III
It's not a bug; it is the way it has always worked.  The CreateFeature method creates the feature in the feature class immediately and returns the Feature object to you.  Any changes to the Feature object that you make will not be written to the feature class until you call the Store method.  So, if you call CreateFeature and set the Shape and other attributes but do not call Store then the Feature object in the feature class will have no geometry and the attributes will remain Null.  The CreateFeatureBuffer method returns a buffer to you.  This buffer is just an in-memory representation of a Feature.  No changes will be made to the feature class until you call the InsertFeature method on the feature cursor.  The documentation is not very clear on any of this.
0 Kudos
SebastianKrings
Occasional Contributor
hey

ok, when it is not a bug the I owuld not say the documentation were not clear enough, I would say the documentation is wrong.

When I say the feature is not stored to the database
until store-method is called
then it must not be stored after calling create
even though it is stored as empty feature.

when this behavior is desired the documentation were clear when saying that the create feature method will directyl store the feature in the featureclass and in the database as well but with null-values. Any value-changes (geometry too) (which are not features in my mind) are only stored after calling the store method.

Thanks to all
0 Kudos