1
votes

Now i am developing a phonegap application:

In which the insertion operation in sqlite db getting locked when app go to background in iOS ( The same worked in android ). When the application is in forground the databse operations work smoothly.

Why this happening and how can i handle this ?

1
might be because iOS does not support multitasking. When the app goes into the background, it is paused; not actually running in the background. While in case of Android there's true multitasking.Varun Mulloli
My application run in background and collect the location data successfully. But the sqlite operations from background only fails.Ajmal M A

1 Answers

0
votes

I think you've used for loop for insertion, Javascript in asynchronous in nature.

You need to execute batch sql queries in the following style not with for loop

db = window.openDatabase("Demo", "1.0", "BusinessApp", 200000);

insertIndex = 0;
var insertCounter = 0;
insertBatch = function(array, arrayLength, tableName, fieldQuestions, cb) {
    console.log("Inserting Record :" + insertCounter);
    if (insertIndex < arrayLength) {
        db.transaction(function(tx) {
            var sql = 'INSERT OR IGNORE INTO ' + tableName + ' VALUES ' + fieldQuestions;
            console.log("sql: " + sql);
            console.log("sql:------------- ");
            console.log(array[insertIndex]);

            tx.executeSql(sql, array[insertIndex],
                function(tx, res) {
                    insertIndex++;
                    console.log("Insert success");
                    insertBatch(array, arrayLength, tableName, fieldQuestions, cb);
                }, function() {
                    console.log("Insert failure");
                });
        });

    } else {
        insertIndex = 0;
        cb();
    }
}

db.transaction(populateDB, errorCB, successCB);


function populateDB(tx) {

    tx.executeSql('DROP TABLE IF EXISTS ?', ["news"], function() {
        console.log("success drop")
    }, function() {
        console.log("failure drop")
    });
    tx.executeSql('CREATE TABLE IF NOT EXISTS news (news_id INTEGER,news_header TEXT,news_content TEXT)');

}

function errorCB() {
    alert("Error: Init.js ErrorCB");
}

function successCB() {
    //alert();
    console.log('DB Created Success');
    var dataArray = []
    dataArray.push([1, "ONE", "First News Content"])
    dataArray.push([2, "TWO", "2 News Content"])
    dataArray.push([3, "THREE", "2 News Content"])
    dataArray.push([4, "FOUR", "4 News Content"])

    insertBatch(dataArray, dataArray.length, "news", "(?,?,?)", success)

    function success() {

        console.log("all success")
    }

}

So Every executeSQL function requires previous executeSQL success callback