Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve
Accepted Solution

Wait for SQLite asynchronous request?

Is there a way to force a wait on a SQLite asynchronous request?

 

I have a situation where I am currently using an alert for debug purposes prior to the return of my resultset. With the alert in place everything works perfectly. When I take out the alert - it gives me back an undefined object.

 

At first I thought it was just a delay introduced by the alert, so I wrote a delay function put it in place and tested it with the same result, undefined object. I even extended the delay to 15 seconds but it still came back with an undefined object.

 

My question is this, the alert/event handler must be doing something other than introducing a delay into the single processing thread. Is there some way to emulate this?

 

I am including my code below.

 

function getMsgInboxList()
{
 var sTime = new Date();
 try
 {
  if ( ! db)
  {
   openDB();
  }
 try
  {
  db.transaction(function (tx)
  {
   tx.executeSql("SELECT * FROM InboundMessages WHERE messagetype IN (0,1,2,3,4)  ORDER BY received DESC;", [], genMsgList);
            }
         );
      }
      catch (Error)
      {
         alert("Fn Err : " + Error);
      }
   }
   catch (Err)
   {
      alert("Fn level2 Error : " + Err);
   }

   var eTime = new Date();
   var el = eTime - sTime;
   
// **This is the alert that seems to control the return of data **
   alert("SQL Function genMsgboxList Elapsed : " + el);
     
   var counter1 = 0;
   do
   {
        pausecomp(100);
        counter1 += 100;
        if (counter1 >= 5000) 
            {
            alert("invalid iHTML string");
            return;
            }
   }
   while(!iHTMLString);
   
   return iHTMLString;
}


function genMsgList (tx2, rs)
{
   var starttime = new Date();
   try
   {
      var len = rs.rows.length, i;
      iHTMLString = "";
      for (i = 0; i < len; i ++ )
      {
         MSGreceived[i]      = rs.rows.item(i).received;

         x = new Date(MSGreceived[i]);
         rYear = x.getFullYear();
         rMonth = x.getMonth();
         rDay = x.getDate();
         rTime = x.toLocaleTimeString();

         rDateString = rMonth + "/" + rDay + "/" + rYear + " " + rTime;
         
         HTMLString = "<div class=\"listItem\" x-blackberry-focusable=\"true\""
         + " onclick = \"openMessage( '" + i + "' ); \">\n"
         + "<div class=\"time\">" + rDateString + "<\/div>\n"
         + "<\/div>\n";

         iHTMLString = iHTMLString + HTMLString;
      }
   }
   catch (Error)
   {
      alert("Callback loop err : \n" + Error);
   }
   var counter2 = 0;
   do
   {
        pausecomp(100);
        counter2 += 100;
        if (counter2 >= 5000) 
            {
            alert("fn2 invalid iHTML string");
            return;
            }
   }
   while(!iHTMLString);

   
   var finishtime = new Date();
   var elapsed = finishtime - starttime;
   //alert("Elapsed SQL callback : " + elapsed);
   return iHTMLString;
}

 

Developer
Posts: 189
Registered: ‎08-13-2008
My Device: Not Specified

Re: Wait for SQLite asynchronous request?

Hello

 

You need to use the call back function on the db.transaction.

 


            aDB.transaction(function(transaction){
                transaction.executeSql('SELECT * FROM ATABLE', [], CallBackHandleResult, errorHandler );
            }, errorHandler, CallBackContinue);

 

            CODE EXECUTED HERE IS NOT ASYNCHRONOUS

 

 

 

** (notice the switch in error handler function and callback function order)

 

So your code after the transaction should go in CallBackContinue.

 

 

Cheers

 

Andrew

Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve

Re: Wait for SQLite asynchronous request?

OK Sorry but I'm not understanding the difference between the two callback functions.

 

Any chance you could put some pseudocode in there so I can get a clue as to what I need to do.

 

Thanks

Developer
Posts: 189
Registered: ‎08-13-2008
My Device: Not Specified

Re: Wait for SQLite asynchronous request?

Hi

 

       aDB.transaction(function(transaction){
            transaction.executeSql('SELECT * FROM ATABLE', [], CallBackHandleResult, errorHandler );

            transaction.executeSql('SELECT * FROM ATABLE2', [], CallBackHandleResult2, errorHandler );
            transaction.executeSql('SELECT * FROM ATABLE3', [], CallBackHandleResult3, errorHandler );
        }, errorHandler, CallBackContinue);

 

     THE CODE HERE IS NOT ASYNCHRONOUS

 

Thats all the pseudo code you need to know really.

 

 

CallBackHandleResult is a function that is called when the select statement is complete.

 You can have multiple SQL statements within the transaction. Each could have a different  CallBackHandleResult function.

 

CallBackContinue is a function that is called when the transaction is complete. i.e. when everything has completed within the transaction.

 

So in your case you are populating iHTMLString in CallBackHandleResult.

But you want to access iHTMLString in CallBackContinue as it is going to call that function AFTER CallBackHandleResult has completed. **NOT in the section of code marked "THE CODE HERE IS NOT ASYNCHRONOUS"

 

Hope that helps

 

Cheers

 

Andrew

Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve

Re: Wait for SQLite asynchronous request?

[ Edited ]

OK so I set up the callbackContinue function. Now I realize that the CallBackHandleResult seems to be returning the undefined String object because when the callbackContinue executes the object is null.

 

Here is what I am using for a callbackContinue - maybe I missed something and I've searched for this syntax and haven't been able to find it yet. This is the first I have seen of a CallBackContinue.

 

Thanks

 

var setTimerVar = 0;
var timerId=setInterval('timer();',500); 


function getMessages()
{
try {
alert("Start getMessages");
    var timer=function (){ 

    if (setTimerVar == "1"){
		alert("setTimerVar = 1");
		return iHTMLString;
            }
        };
    alert("SetTimerVar :" + setTimerVar);
    }
    catch(Error)
    {
    alert("Timer function err" + Error);
    }
}

 

Developer
Posts: 189
Registered: ‎08-13-2008
My Device: Not Specified

Re: Wait for SQLite asynchronous request?

Hi

 

As a test, have you tried something simple like this?

 

var iHTMLString;
//global variable

 

function getMsgInboxList() {
  if (!db) {
   openDB();
  }

  db.transaction(function (tx) {
   tx.executeSql("SELECT * FROM InboundMessages WHERE messagetype IN (0,1,2,3,4)  ORDER BY received DESC;", [], CallBackHandleResult);
  },errorHandler, CallBackContinue);
}

 

function CallBackHandleResult(tx2, rs) {
 for (i=0; i < rs.rows.length;i++){
  iHTMLString = iHTMLString + '<br>';
 }
}

 

function CallBackContinue() {
 alert(iHTMLString);
}

 

Check if this works first, then add code.

 

Cheers Andrew

Developer
Posts: 67
Registered: ‎09-23-2010
My Device: Curve

Re: Wait for SQLite asynchronous request?

As long as I do my DOM modifications in CallBackContinue it all works.

 

Thank you so much for your help - you rock!