From: Andrea A. <aa...@op...> - 2007-07-16 12:08:34
|
Gertjan van Oosten ha scritto: > Hi developers, > > I'm getting unexpected behaviour from FeatureCollection. > Basically, what I'm doing is this: > > final FeatureCollection featuresBeforeInsert = > getAllFeatures(connectionString, typeName); > > insertFeature(connectionString, typeName, featureAttributes); > > final FeatureCollection featuresAfterInsert = > getAllFeatures(connectionString, typeName); > > assertEquals(featuresBeforeInsert.size(), featuresAfterInsert.size()-1); > > where getAllFeatures() retrieves all features of the given type from the > PostGIS data store, and insertFeature inserts one feature of the given > type with the given attributes in the data store. > > The assert fails, because both collections featuresBeforeInsert and > featuresAfterInsert have the same size. Apparently, the insertFeature > also changes the collection under the hood of the featuresBeforeInsert... > That is rather unexpected. Is this intended behaviour? Should I have > been aware of it? Is it documented somewhere? And more importantly: > how do I get a FeatureCollection that does not get changed underneath me? Unfortunately this is intended behaviour :( FeatureCollection is a persistent collection, whatever you do against it, you do against your persistent data. Usually its memory footprint is close to zero, that is, each time you try and access it, it'll reload data from the store, and so on. All that is gives you is a collection like access to your storage, but it's not an in memory snapshot like one would expect (at least, like me and you would expect). If you need an in memory copy of the data instead, it should work if you create a DefaultFeatureCollection : FeatureCollection dstoreColl = myFeatureSource.getFeatures(); DefaultFeatureCollection inMemoryColl = new DefaultFeatureCollection(null, dstoreColl.getType()); inMemoryColl.addAll(dstoreColl); Hope this helps Cheers Andrea |