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
Contributor
Posts: 31
Registered: ‎01-06-2011
My Device: 9300
My Carrier: MTN Nigeria

for loops and OS 6 asynchronous db access

I learnt this the hard way and thought to post it here to the forum. It may be fairly common to carry out an operation like the below:

 

 

db.transaction(function(tx){
    for(var i = 0; i < some_max; i++)
    {
        tx.execute('some_sql', [i, some_other_var_based_on_i,...]);
    }
})

 

or its counterpart:

 

 

for(var i = 0; i < some_max; i++)
{
    db.transaction(function(tx){
        tx.execute('some_sql', [i, some_other_var_based_on_i,...]);
    });
}

 

 

The problem that may be encountered with asynchronous db transaction however is that the loops would have passed their current iteration before the transaction executes thus using a wrong 'i' in the transaction. Both snippets can be transformed to overcome this issue thus:

 

 

db.transaction(function(tx){
    for(var i = 0; i < some_max; i++)
    {
        (function(tx, i, some_other_var_based_on_i,...){
            tx.execute('some_sql', [i, some_other_var_based_on_i,...]);            
        })(tx, i, some_other_var_based_on_i,...);
    }
})

 

and

 

 

for(var i = 0; i < some_max; i++)
{
    db.transaction(function(tx){
        (function(tx, i, some_other_var_based_on_i,...){
            tx.execute('some_sql', [i, some_other_var_based_on_i,...]);
        })(tx, i, some_other_var_based_on_i,...);
    });
}

 

This works since the variables used in the transaction don't change with every iteration of the loop. I'm not sure though if you can do without the reference to 'tx' being passed to the inner functions. 

 

To keep the code from looking complex, I also believe the functions can be refactored out to stand-alone functions.

 

 

Cheers!
Ezeozue Chidube
Seamfix Nigeria Limited (www.seamfix.com)
www.twitter.com/cezeozue
Developer
Posts: 84
Registered: ‎06-26-2008
My Device: Not Specified

Re: for loops and OS 6 asynchronous db access

try with a recursive operation, it's what i do.

 

 

Contributor
Posts: 31
Registered: ‎01-06-2011
My Device: 9300
My Carrier: MTN Nigeria

Re: for loops and OS 6 asynchronous db access

And lose the for loop altogether?

Cheers!
Ezeozue Chidube
Seamfix Nigeria Limited (www.seamfix.com)
www.twitter.com/cezeozue
Developer
Posts: 84
Registered: ‎06-26-2008
My Device: Not Specified

Re: for loops and OS 6 asynchronous db access

Something like this

 

var sqlArray= [{sql:'insert....', args:[arg1,arg2]}];

 

db.transaction(function(fx){

   save(tx, sqlArray);

}Smiley Wink

 

save = function(tx, sqlArray){

if (sqlArray.length>0){

  var sql = sqlArray.pop();

  tx.execute(sql.sql, sql.args, function(tx2,rs){

    save(tx, sqlArray);

 }

}else{

   return true;

}

}

Contributor
Posts: 31
Registered: ‎01-06-2011
My Device: 9300
My Carrier: MTN Nigeria

Re: for loops and OS 6 asynchronous db access

I understand; you accumulate the sql statements, their other arguments, etc in an array while in the loop and recursively go through the array outside the loop.

 

Why not just loop through the array containing the statements instead of using recursion?

Cheers!
Ezeozue Chidube
Seamfix Nigeria Limited (www.seamfix.com)
www.twitter.com/cezeozue
Developer
Posts: 84
Registered: ‎06-26-2008
My Device: Not Specified

Re: for loops and OS 6 asynchronous db access

becouse htm5 db is asynchronous, if you make it recursive, you wait until the end of the first transaction, to execute the second one. If you make a loop, you are sending all the statements to the db first, and then the dabase will execute it.

 

 

 

 

Contributor
Posts: 31
Registered: ‎01-06-2011
My Device: 9300
My Carrier: MTN Nigeria

Re: for loops and OS 6 asynchronous db access

Yes. But even in a loop, the statements are executed in the order in which they are queued so you achieve about the same effect as with recursion.

Cheers!
Ezeozue Chidube
Seamfix Nigeria Limited (www.seamfix.com)
www.twitter.com/cezeozue
Developer
Posts: 84
Registered: ‎06-26-2008
My Device: Not Specified

Re: for loops and OS 6 asynchronous db access

yes, but if you get an error in teh first one, how will you handle it?

 

Will the db rollback all statements or not?

 

How do you know where are all the statements executed?

 

 

Contributor
Posts: 31
Registered: ‎01-06-2011
My Device: 9300
My Carrier: MTN Nigeria

Re: for loops and OS 6 asynchronous db access

Each call to tx.executeSql will have its error-handling callback.

As for rollback, the calls to tx.executeSql are still within a wrapping db.transaction(...) so rollback will take place as normal.

I don't understand what you mean by 'where' the statements are executed.

Cheers!
Ezeozue Chidube
Seamfix Nigeria Limited (www.seamfix.com)
www.twitter.com/cezeozue