01-26-2012 04:48 AM
Hi,
We've run into a strange situation. We're calling a database.transaction(txCallback, txError, txSuccess),
and if we follow the transaction call with an alert() call, the txSuccess function is called without the
txCallback function ever being called.
Is this a known error, or documented behaviour with a reasonable explanation?
This HTML demonstrates the situation well in Ripple Emulator:
<html>
<head>
<script>
function dbalert() {
var db = window.openDatabase("test","1.0","test",1024*1024)
console.log("Next line should read: In transaction callback");
window.transactionCalled = false;
db.transaction(
function (tx) {
console.log("In transaction callback");
window.transactionCalled = true;
},
function (tx, err) {
console.error("ERROR");
console.log(err);
},
function () {
if (window.transactionCalled) {
console.log("Success callback: everything worked!");
} else {
console.error("Success callback: BUT TRANSACTION WAS NEVER CALLED");
}
}
);
/*****
* Change to FALSE to get this working.
*****/
if (true) {
alert("Ok, let's see what happened");
} else {
console.log("Ok, let's see what happened");
}
}
</script>
</head>
<body onLoad="dbalert();">
<div id="out">
</div>
</body>
</html>
02-01-2012 03:50 PM
HTML5 database is asyncronous. When you introduce the modal alert() dialog box, it forces the main thread to stop and wait, while the DB thread continues its action. The code you have shown appears to be functioning as it should be - dbalert function being called from the onload event, immediately start a transaction; the alert forces the app to wait and in that time the transaction completes.
FYI: here's another sample page that includes a demo of how to use transactions:
http://blackberry.github.com/WebWorks-Samples/kitc