1

When you reach the size limit for your web sql store, Mobile Safari (and the Android browser) will prompt you to increase the storage size. Once that happens, neither the transaction, onSuccess callback or onError callback is executed. I cannot seem to catch any exceptions here either, so how am I supposed to handle prompts for increased storage?

All the operations are async, so I can only think of setting a timeout and checking if the transaction completed after some time has gone. Which of course is a nasty, bug-ridden hack. And in addition to that, I have to re-do the transaction to actually check if the space was increased or not.

Fiddle for verifying on mobile browser

    // open the jsfiddle in safari

    // refuse when prompted to increase space to show the bug
    db.transaction(function (tx) {
        tx.executeSql("INSERT INTO key_value(key, value) VALUES(?,?);", ["mykey", buildBigString(3*Math.pow(10,6))], function (tx, result) {
        // will never be called
        done();
        }, function (tx, error) {
        // will never be called
            done();
        });
    });
4

1 回答 1

1

上面代码的问题基本上是我错过了包装 sql 插入的事务的错误回调。有关一般如何实际处理用户提示的更多信息,请参阅有关此问题的详细博客文章

来自异步数据库 API 的规范

void transaction(in SQLTransactionCallback callback, 
                 in optional SQLTransactionErrorCallback errorCallback,
                 in optional SQLVoidCallback successCallback);

没有示例代码显示这一点,因此我继续假设该transaction方法是在没有回调的情况下使用的。

所以不要写

db.transaction(function (tx) {
 // if this prompts the user to increase the size it will not execute the sql or run any of the callbacks
 tx.executeSql('INSERT INTO foo (id, text) VALUES (1, createReallyBigString('4MB')', onComplete, onError );
 });

做这个

// Note the reverse order of the callbacks!
 db.transaction(function (tx) {
 // this will prompt the user to increase the size and the sql will not be executed
 tx.executeSql('INSERT INTO foo (id, text) VALUES (1, createReallyBigString('4MB')');
 }, onError, onComplete );
于 2013-08-06T13:23:29.683 回答