function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
henryCHhenryCH 

Flex: QueryResultIterator

Hello everybody,

I'd like to display a pop-up window with a progress bar on it, while larger amounts of data is retrieved by the QueryResultIterator but I can't find out when exactly the asynchronous QueryResultIterator finishes. So I also don't know when the pop-up window can be removed. Any suggestions??

Thanks and best regards,
Henry
henryCHhenryCH
Just in case somebody cares, a little workaround that seems to work fine:

Code:
public function query(soql:String, result:ArrayCollection):void {
 var count:int = 0;
 apex.addEventListener(QueryEvent.QUERY_EVENT, handleQueryCompleted);
 progressWin.visible = true; // displays a TitleWindow with a progress bar on it
 var d:QueryResultIterator = new QueryResultIterator(apex, soql,
  function (so:SObject):Boolean {
   result.addItem(so);
   return true;
  }
 );
}
   
private function handleQueryCompleted(event:QueryEvent):void {
 if(event.queryResult.done){
  progressWin.visible = false;
} }


 

Message Edited by henryCH on 06-13-2007 09:24 AM

Ron HessRon Hess
this looks like it will work, but i'm not sure.

the QUERY_EVENT will be send for each batch, not at the end of the last batch.

you may be removing the wait window too soon.
the callback really needs access to the queryResult.done flag.

i'll look at re-writing this iterator to look more like the ajax version.

until then you can modify your library and pass the QR to your call back,
here is what i'm thinking of (inside queryMoreProcessBatch() ), note this passes the record and the full result to allow lookahead or insight into the value of qr.done.

Code:
   for (var i:int=0;i<qr.records.length;i++) { 
    // make the callback 
    if ( ! this._foreach( qr.records[i], qr ) ) { 
     userDone = true; // user's function returned false, stop processing 
    break;
    }
   }

 

also this method could send another event on "really done";


henryCHhenryCH
Hey Ron,

thanks for the response. I think I ran into another problem with QueryResultIterator. Whenever I tried to execute an SOQL statement that returns 0 records a TyperError caused by a null reference is thrown. So I checked the code and it seems that the case of zero records was not checked:

Code:
private function queryMoreProcessBatch(event:QueryEvent):void {
    var qr:QueryResult = event.queryResult;  
    var userDone:Boolean = false;

    // if qr.size 0 then qr.records is null which results in an exception in the following loop

    for (var i:int=0;i<qr.records.length;i++) { 
     // make the callback 
     if ( ! this._foreach( qr.records[i] ) ) { 
      userDone = true; // user's function returned false, stop processing 
      break;
     }
    }

 I'm not sure if this is a bug, so please advise.

Thanks and best regards,
Henry
Ron HessRon Hess
yes, this is a bug, thanks for reporting.
I should be able to put out a new library with a fix next week.
henryCHhenryCH
Hey Ron,

the problem with handling the QUERY_EVENT was that the event is triggered before the records are processed, so at the time the query was completed the foreach-Function has not been started. I think it's really necessary to know when the whole query/queryMore process finished so I added another QueryEvent type: QUERY_DONE to the QueryEvent class, which is fired at the following postition in the QueryResultIterator class:

Code:
// setup for another event if we are not done
if ( userDone == false && qr.done == false ) {
this._connection.queryMore( qr.queryLocator, new AsyncResponder(sendQueryComplete, this.genericFault ));
} else {
sendQueryDone(qr);
_connection.removeEventListener(QueryEvent.QUERY_COMPLETE, queryMoreProcessBatch); // all done, clean up
}

This finally seems to work, but I think it's a kind of a dirty hack. So do you think there will be an indicator for the completion of the query process in future versions?

Thanks again and best regards
Henry

jf317820jf317820
what's the workaround for the "TypeError caused by a null reference" when a query returns a null value?  my problem is I have a query with a nested query and whenever the nested query returns zero records, the type error, null reference is thrown.

thanks in advance
Ron WildRon Wild
Has there been a fix published for this bug?  I downloaded the Flex toolkit today and still get:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

on queries with 0 record results.

Thanks,

henryCHhenryCH

hey ron,

i just had a look at the latest release of the flex toolkit and i think the issue has already been fixed:

Code:

private function queryMoreProcessBatch(event:QueryEvent):void {
    this._qr = event.queryResult;
    var userDone:Boolean = false;

    if (this._qr.size > 0) { // size is 0 if query found no records
     for (this._index = 0; this._index < this._qr.records.length; this._index++) { 
      // make the callback 
      if ( ! this._foreach( this._qr.records[this._index] ) ) { 
       userDone = true; // user's function returned false, stop processing 
       break;
      }
     }
    }

...

Best regards,

Henry