Simon Fell > Its just code > Sforce / .NET tip for queryMore

Thursday, March 10, 2005

A lot of code using the Sforce API deals with calling query and queryMore, probably using something like this

sforce.QueryResult qr = svc.query("Select Id, Name from Account");
do
{
	foreach (sforce.Account a in qr.records)
	{
		processAccount(a);
	}
	if (!qr.done)
		qr = svc.queryMore(qr.queryLocator);
	else
		break;
} while (true);

But the .NET Web Services client stack gets an async client API for free, this lets you easily start the queryMore call before you start the job of processing the current records, so when you've done with the current records, the next are ready for process, no need to wait around for the queryMore call to hit the network and comeback. If you're processing millions of records, then this can add up, here's a revised approach

sforce.QueryResult qr = svc.query("select Id, Name from Account");
IAsyncResult asyncQueryMore = null;
do
{
	if (asyncQueryMore != null) qr = svc.EndqueryMore(asyncQueryMore);
	if (!qr.done) asyncQueryMore = svc.BeginqueryMore(qr.queryLocator, null, null);
	foreach ( sforce.Account a in qr.records)
	{
		processAccount(a);
	}
} while (!qr.done);