簡體   English   中英

當應用程序進入后台時,iOS Phonegap sqlite數據庫被鎖定

[英]iOS Phonegap sqlite database getting locked when app go to background

現在,我正在開發一個phonegap應用程序:

當app進入iOS后台時,sqlite db中的插入操作被鎖定(在android中也是如此)。 當應用程序處於前台時,數據庫操作將順利進行。

為什么會這樣,我該如何處理?

我認為您已經使用了插入循環,而Javascript本質上是異步的。

您需要以以下樣式執行批處理sql查詢,而不是使用for循環

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")
    }

}

因此,每個executeSQ L函數都需要先前的executeSQL成功回調

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM