【20100105-01】IFeatureClass.Search(IQuery Filter,bool Recycling)参数说明

IFeatureClass的Search方法大家经常用到,很多人对Search方法的其中一个参数bool Recycling不是很理解。
目前网上关于这个参数的意义的解释有两个版本:
1.第一版本是IsNotNull大牛的:
http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=13461
他提出的观点的是:参数Recycling为True的时候是传引用,为False的时候为传值
2.第二个版本是Echo兄的:
http://gis.cnblogs.com/home.aspx?page=3
他提出的观点是:参数Recycling为True的时候游标对象(Ifeaturecursor)是只包含一条记录,为False的时候游标对象(Ifeaturecursor)为10条记录(假设Featureclass有10条记录)

我经过测试提出个人的观点:
1.pFeatureCursor存储的是所有符合条件的Feature对象的引用,
2.Recycling的意思是回收,参数Recycling为True的时当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值在内存中所占的地址就会被销毁回收,为False的时候当执行这个方法IFeature pFeature=pFeatureCursor.NextFeature()上一条记录的值依然存在在内存中。
Ok,接下来以实例证明本观点:

1.参数Recycling设置为True
           IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);

            List<IFeature> pFeatures = new List<IFeature>();
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                pFeatures.Add(pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }
            
            for (int i = 0; i < pFeatures.Count; i++)
            {
                MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString());
            }
分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为TRUE也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中被回收,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都为Null。所以会引发一下错误(如下图所示):




错误.jpg

 

2.参数Recycling设置为False
           IFeatureClass pFeatureClass = (this.axMapControl1.get_Layer(0) as IFeatureLayer).FeatureClass;
            //IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);

            List<IFeature> pFeatures = new List<IFeature>();
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                pFeatures.Add(pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }
            
            for (int i = 0; i < pFeatures.Count; i++)
            {
                MessageBox.Show(((pFeatures.Shape as IPolygon) as IArea).Area.ToString());
            }
分析:pFeatures集合存储了指向FeatureClass上所有Feature的引用,但是由于Recycling参数设置为False也就是说每执行一个NextFeautre方法上一条记录的Feature值在内存中依然存在,所以到最后遍历pFeatures集合的时候所有的IFeature引用指向的Feature对象都依然存在。所以会执行的很Happy(如下图所示):


正确.jpg



 

转自 fxlcocohttp://www.gisall.com/?uid-4359-action-viewspace-itemid-587

 

因此在性能方面,参见:http://www.cnblogs.com/wall/archive/2008/05/07/1186203.html

 

帮助中有如此论述,指出recycling参数的主要性:

The Recycling parameter controls feature object allocation behavior. Recycling cursors rehydrate a single feature object on each fetch and can be used to optimize read-only access, for example, when drawing. It is illegal to maintain a reference on a feature object returned by a recycling cursor across multiple calls to NextFeature on the cursor. Feature objects returned by a recycling cursor should not be modified.

Non-recycling cursors return a separate feature object on each fetch. The objects returned by a non-recycling cursor may be modified and stored with polymorphic behavior. The geodatabase guarantees 'unique instance semantics' on non-recycling feature objects fetched during an edit session.

Recycling cursors should be used only for drawing and read-only access to object state. Use non-recycling search cursors to fetch objects that are to be updated.

通过下面的函数可以测试该参数的重要性:

Const N = 100000
 

Sub TestRecycle()
Dim amap As IMxDocument
Set amap = ThisDocument

  Dim lyr As IFeatureLayer
  Set lyr = amap.FocusMap.Layer(0)
  Dim fc As IFeatureCursor
 
  Dim qr As IQueryFilter
  Set qr = New QueryFilter
  qr.WhereClause = "objectID < " & N
  Dim dt As Date
 
  Debug.Print "Recyle: False"
  dt = Now
  Set fc = lyr.Search(qr, False)
  While Not fc.NextFeature Is Nothing
 
  Wend
Debug.Print "Seconds: " & DateDiff("s", dt, Now)
 
  dt = Now
  Debug.Print "Recyle: True"
  Set fc = lyr.Search(qr, True)
  While Not fc.NextFeature Is Nothing
 
  Wend
Debug.Print "Seconds: " & DateDiff("s", dt, Now)
 
End Sub

结果如下:

N=100000

Recyle: False
Seconds: 3
Recyle: True
Seconds: 1

N=500000

Recyle: False
Seconds: 12
Recyle: True
Seconds: 8

说明在只读查询中,将Recycling参数设为true可以提高函数效率。

posted @ 2010-01-05 14:16  WillWayer  阅读(688)  评论(1编辑  收藏  举报