Tuesday, August 4, 2009

When to use a Recycling Cursor

If you are new to ArcObjects, you must have seen this Boolean parameter when you want to create a search cursor. This might be confusing for some new developers. I didn't know about it in the beginning so I used to insert a false there every time.

Actually this very Boolean parameter can really ruin your entire application and produce unexpected results if used in the wrong context.

What is Recycling?
Recycling is reusing the same memory location for creating features or rows.
To make a long story short, I'm going to explain it with pictures.

Here is a picture with a non-Recycling Cursor

Click the image to enlarge


Here is another one with a Recycling Cursor


Click the image to enlarge



When to use Recycling?
Recycling cursor is much more faster than non-recycling one. It is great for populating lists and printing values in the screen.

Here are some cases you want to use Recycling in
  • Populating Features' attributes in a list
  • Saving certain primitive features attributes (like objectid) in a collection

When not to use Recycling?
When capturing the feature or its geometry (or any other non-primitive reference) , DO NOT use Recycling cursor. If you did you will simply get the same feature (probably the last one) in all your collection because it is using the same memory location.

Here are some cases you must not use Recycling in
  • Union the geometry of all features in a given cursor
  • Saving the feature geometry in the Grid Tag or collections
  • Comparing features

Hope that helped!