Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Getting data from a Datasource into a RecordList

    Hi,

    I'm stuck on a seemingly simple problem. All I'm trying to do is to get the data from a datasource (processDS) into a RecordList (procRecordList). This is my (simplified) code:

    Code:
    // processDS is defined outside the method
    
    public void loadData()
    {		
        	final RecordList procRecordList = new RecordList();
        	
        	processDS.fetchData(null, new DSCallback() {
                public void execute(DSResponse response, Object rawData, DSRequest request) {   
                	procRecordList.addList(response.getData());
                	SC.say(""+procRecordList.getLength()); // This prints the correct number of records, which are there in the datasource 
                	
                }
    	});
            
    	SC.say(""+procRecordList.getLength());  // This prints zero
    }
    I can get the records inside the execute method, but after the method, the records are gone.

    I think I'm missing something very basic here, but just can't seem to figure it out. Any help will be appreciated.

    I'm using SmartGWT 2.2 with Firefox 6.5.

    Thanks in advance.

    #2
    The fetchData() call is not synchronous so the second SC.say is looking at the recordlist before the data is received. The execute() method of the callback is the correct location to deal with returning data.

    Of course you could simplify by using
    Code:
    procRecordList = response.getDataAsRecordList();

    Comment


      #3
      Hello davidj6,

      Thanks a lot for your response. I really appreciate it.

      Now I at least understand why it isn't working. What would be a good way to wait for the asynchronous fetchData call to complete, or is there any alternate approach to populate the record list with data from the data source?

      I did use getDataAsRecordList(), but then I had to make procRecordList as a non-local (and of course non-final) variable in the class.

      Thanks again for your help.

      Comment


        #4
        You are doing the correct thing to wait on the completion: the DSCallback.execute(). Since you can't use procRecordList outside the DSCallback (because you don't have the data) you just need to do what you want inside .execute() or call another procedure from there with the recordlist.

        I guess the question is, what are you trying to do with the response data?

        Comment


          #5
          Hello davidj6,

          Thanks again for your quick response.

          Originally posted by davidj6
          You are doing the correct thing to wait on the completion: the DSCallback.execute(). Since you can't use procRecordList outside the DSCallback (because you don't have the data) you just need to do what you want inside .execute() or call another procedure from there with the recordlist.
          I also had this alternative in mind too, but what held me back from using it was that the fact that I had 2 datasources. So finally what I've done is this:

          Code:
          firstDS.fetchData(null, new DSCallback() {
                              public void execute(DSResponse response, Object rawData, DSRequest request) {                      	
                              	firstRecordList = response.getDataAsRecordList();
                                  
                              	secondDS.fetchData(null, new DSCallback() {
                                      public void execute(DSResponse response, Object rawData, DSRequest request) {            	
                                      	secondRecordList = response.getDataAsRecordList();                            	
                                      	someMethod();
                                      }
                         		});    	
                              }
                 		});
          I didn't like the idea of nesting the fetchData like I've shown above, but the important thing is that it works. Please let me know if there's a better way to do it.

          I really appreciate your help and patience in working through with me. Thanks a lot.

          Comment

          Working...
          X