08-05-2011 05:24 PM - edited 08-05-2011 05:24 PM
Hi there. I am trying to build an application that will work on both OS5 and OS6. I am trying to store some data to local memory using databases. I have successfully implemented this in OS5 using the html5_init.js file which converts it into gears. The problem is that is does not work for OS6.
Each time I try to run the application, it crashes. The blackberry simulator just stops responding. I have experienced this in simulators 9800 (v.534) and 9700 (v.570). I do simulate an SD card as that is required for databases.
Here is a dummed down version of the code which fails.
config.xml
<?xml version="1.0" encoding="UTF-8"?> <widget xmlns="http://www.w3.org/ns/widgets" xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0.0" rim:header="IESOAPP"> <name>MAAG</name> <description>MAAG test appplication.</description> <author>thats me</author> <content src="sample.html"/> <access uri="http://hello.ca" subdomains="true" /> </widget>
sample.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>Test</title>
<!-- path to the script files changed for codepresentation -->
<meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no" />
<meta name="x-blackberry-defaultHoverEffect" content="false" />
<script type="text/javascript">
var db = null;
function setupData() {
alert('am setting it up!');
try {
db = openDatabase("TestDB", '1.0', 'Test', 50 * 1024);
// db = new Database("TestDB"); // fails with the mentioned exception (not defined)
if (db) {
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS userdata (key int unique, value text)");
tx.executeSql("SELECT value FROM userdata WHERE key = ?", [1], function (tx, results) {
if (results.rows.length == 1) {
document.getElementById("storageField").value = results.rows.item(0).value;
}
else {
tx.executeSql("INSERT INTO userdata (key, value) VALUES (?, ?)", [1, ""]);
}
alert("loaded");
});
});
}
else {
document.getElementById("txt").value += "\r\nNo DB\r\nopenDatabase:\r\n" + openDatabase;
}
}
catch (err) {
document.getElementById("txt").value += err;
}
}
function store()
{
if (!db)
{
alert("No DB!");
return;
}
var data = document.getElementById("storageField").value;
db.transaction(function (tx) {
tx.executeSql("UPDATE userdata SET value = ? WHERE key = ? ", [data, 1]);
});
}
</script>
</head>
<body onload="setupData()">
<div>
<input type="text" id="storageField" /><input type="button" value="Store" onclick="store();" />
<br />
<textarea style="width: 100%;" rows="10" id="txt"></textarea>
</div>
</body>
</html>
Can someone please help me to resolve this issue. I really need to save to local memory in both OS versions and this should work in OS6.
08-08-2011 09:34 AM
08-08-2011 11:36 AM
09-15-2011 06:58 PM
I am having this problem. My application uses sqlite database and it crashes the app, it works most of the time but eventually when the app accesses the database the app crashes and when I try to restart it never starts again, I have to remove battery and reset the phone. I am running it on a torch with os6.0. When I reset the phone it works for a bit and then eventually crashes again. I am testing on an actual device. The same application runes on my curve with the 5.0 os and no problems. Has anyone else solved this problem? I am using the latest SDK 2.01. Help. I will have to scrap the whole project if I cant get this fixed.
09-16-2011 11:25 AM
kingsha -You do not have all your transactions chained so there's no guarantee that the database has been created before the next transaction is fired. Admittedly that shouldn't crash the app though.
bbooks- what versions of the simulator are you using?
button below the post(s)09-16-2011 12:42 PM
I am not using simulator. I am testing on actual devices. I have found that the simulators take longer to load and test than on actual device. I am now trying to upgrade to sdk 2.0.1.1 to see if this solves problem. I read somewhere that this has fixed some memory leak problems.
09-16-2011 06:06 PM
I read here http://supportforums.blackberry.com/t5/Web-and-Web
So only solution is to wait for fix or go the java route.
I tried sdk 2.0.1.1 still same problem!
09-17-2011 02:09 AM
Hi Guys
HTML5 database are asynchronous.
You can't call transactions for select statements in the same thread as creating the database tables.
The tables may not be created yet.
So the following will casue errors in v6 devices.
tx.executeSql("CREATE TABLE IF NOT EXISTS userdata (key int unique, value text)");
tx.executeSql("SELECT value FROM userdata WHERE key = ?", [1], function (tx, results) {
if (results.rows.length == 1) {
document.getElementById("storageField").value = results.rows.item(0).value;
}
else {
tx.executeSql("INSERT INTO userdata (key, value) VALUES (?, ?)", [1, ""]);
}
alert("loaded");
});
You need to call the select on the callback of the creation of tables.
You also need to handle database errors.
e.g.
As an example of async and callbacks check this out
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>Test</title>
<!-- path to the script files changed for codepresentation -->
<meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no" />
<meta name="x-blackberry-defaultHoverEffect" content="false" />
<script type="text/javascript">
var db = null;
function setupData() {
alert('am setting it up!');
try {
db = openDatabase("TestDB", '1.0', 'Test', 50 * 1024);
// db = new Database("TestDB"); // fails with the mentioned exception (not defined)
if (db) {
db.transaction(function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS userdata (key int unique, value text)", [],DataHandler,errorHandler);
},
tx_errorHandler
,
tx_DB_Select
);
}
else {
document.getElementById("txt").value += "\r\nNo DB\r\nopenDatabase:\r\n" + openDatabase;
}
}
catch (err) {
document.getElementById("txt").value += err;
}
}
function errorHandler(transaction, error){
try {
alert('errorHandler-message:' + error.message + '. code:' + error.code + '.');
}
catch(e){
alert('errorHandler-error:' + e.message + '.');
}
}
function DataHandler(transaction, results){
try {
if (results.rows.length == 1) {
document.getElementById("storageField").value = results.rows.item(0).value;
}
else {
//tx.executeSql("INSERT INTO userdata (key, value) VALUES (?, ?)", [1, ""]);
}
alert("loaded");
}
catch(e){
alert('DataHandler-error:' + e.message + '.');
}
}
function tx_errorHandler(error){
try {
alert('tx_errorHandler-message:' + error.message + '. code:' + error.code + '.');
}
catch(e){
alert('tx_errorHandler-error:' + e.message + '.');
}
}
function tx_DB_Select(){
try {
db.transaction(function(tx){
tx.executeSql("SELECT value FROM userdata WHERE key = ?", [1],DataHandler,errorHandler);
},
tx_errorHandler
,
tx_DataHandler
);
}
catch(e){
alert('tx_DB_Select-error:' + e.message + '.');
}
}
function tx_DataHandler(){
try {
}
catch(e){
alert('tx_DataHandler-error:' + e.message + '.');
}
}
function store()
{
if (!db)
{
alert("No DB!");
return;
}
var data = document.getElementById("storageField").value;
db.transaction(function (tx) {
tx.executeSql("UPDATE userdata SET value = ? WHERE key = ? ", [data, 1]);
});
}
</script>
</head>
<body onload="setupData()">
<div>
<input type="text" id="storageField" /><input type="button" value="Store" onclick="store();" />
<br />
<textarea style="width: 100%;" rows="10" id="txt"></textarea>
</div>
</body>
</html>
Cheers
Andrew
09-18-2011 10:45 AM
Thanks for your code abarber.
I am doing something similar in my code and still experience memory leak error. As I said before the error I believe is rooted in a memory leak problem associated with webworks on the 6.0 os. The webworks does not release all the memory it should when the application is closed. We encountered the problem when stress testing application before launching it. If the application is opened and closed a number of times it will eventually crash with low memory error. This occurs not only with app that use database. It seems to occur with most apps with images and or DOM manipulation etc.. It seems that rim is aware of the problem and are trying to fix it. We have had to shelved the project until rim releases fix.
Can anyone else try to stress test their app on Os 6 and see if they experience the same.
Regards
09-19-2011 11:18 AM
Good news / Bad news
Good news: Rim knows about memory leak problem and say they have a fix that they are testing that will trick webkit into releasing memory, that should fix problem. You will have to recompile your app with a new sdk.
Bad news: the fix will not be available until mid October.