自iOS6发布以来,我的网络应用程序遇到了一系列错误,其中最糟糕的是我几乎100%肯定的是webql事务正在排队。当我第一次在移动safari(ipad)中加载应用程序时,交易正常。然后,如果我关闭safari并再次打开它,则事务似乎排队并且永远不会执行。
如果我打开开发工具并运行一个简单的alert
,方法将触发,如果我只是重新加载事务也可以正常工作,或者如果我将数据库事务的运行延迟1秒或者某事它的工作正常。
我不想运行setTimeout来运行事务。
这是safari现在实施后的缓存问题吗?
如果有人对如何解决这个问题有任何好的想法,请在下面回答。
提前致谢。
答案 0 :(得分:0)
可能不是错误。您可能不必要地使用一系列交易。您可以为每个事务使用多个请求。 onsuccess
回调,您可以重复使用该事务。它应该工作。同时,限制每个事务的请求数。永远不应该setTimeout
。
以下是使用单个事务插入多个对象的方法
/**
* @param {goog.async.Deferred} df
* @param {string} store_name table name.
* @param {!Array.<!Object>} objects object to put.
* @param {!Array.<(Array|string|number)>=} opt_keys
*/
ydn.db.req.WebSql.prototype.putObjects = function (df, store_name, objects, opt_keys) {
var table = this.schema.getStore(store_name);
if (!table) {
throw new ydn.db.NotFoundError(store_name);
}
var me = this;
var result_keys = [];
var result_count = 0;
/**
* Put and item at i. This ydn.db.core.Storage will invoke callback to df if all objects
* have been put, otherwise recursive call to itself at next i+1 item.
* @param {number} i
* @param {SQLTransaction} tx
*/
var put = function (i, tx) {
// todo: handle undefined or null object
var out;
if (goog.isDef(opt_keys)) {
out = table.getIndexedValues(objects[i], opt_keys[i]);
} else {
out = table.getIndexedValues(objects[i]);
}
//console.log([obj, JSON.stringify(obj)]);
var sql = 'INSERT OR REPLACE INTO ' + table.getQuotedName() +
' (' + out.columns.join(', ') + ') ' +
'VALUES (' + out.slots.join(', ') + ');';
/**
* @param {SQLTransaction} transaction transaction.
* @param {SQLResultSet} results results.
*/
var success_callback = function (transaction, results) {
result_count++;
result_keys[i] = goog.isDef(out.key) ? out.key : results.insertId;
if (result_count == objects.length) {
df.callback(result_keys);
} else {
var next = i + ydn.db.req.WebSql.RW_REQ_PER_TX;
if (next < objects.length) {
put(next, transaction);
}
}
};
/**
* @param {SQLTransaction} tr transaction.
* @param {SQLError} error error.
*/
var error_callback = function (tr, error) {
if (ydn.db.req.WebSql.DEBUG) {
window.console.log([sql, out, tr, error]);
}
df.errback(error);
return true; // roll back
};
//console.log([sql, out.values]);
tx.executeSql(sql, out.values, success_callback, error_callback);
};
if (objects.length > 0) {
// send parallel requests
for (var i = 0; i < ydn.db.req.WebSql.RW_REQ_PER_TX && i < objects.length; i++) {
put(i, this.getTx());
}
} else {
df.callback([]);
}
};
关于事务队列,最好由应用程序处理,而不是由SQLite处理以获得健壮性。基本上我们可以在开始新事务之前观察事务complete
事件。只要它们受到控制,运行多个事务也很好。失控将在循环下打开交易。一般来说,我只会开几笔交易。
以下是交易排队的方式:
/**
* Create a new isolated transaction. After creating a transaction, use
* {@link #getTx} to received an active transaction. If transaction is not
* active, it return null. In this case a new transaction must re-create.
* @export
* @param {Function} trFn function that invoke in the transaction.
* @param {!Array.<string>} store_names list of keys or
* store name involved in the transaction.
* @param {ydn.db.TransactionMode=} opt_mode mode, default to 'readonly'.
* @param {function(ydn.db.TransactionEventTypes, *)=} oncompleted
* @param {...} opt_args
* @override
*/
ydn.db.tr.TxStorage.prototype.transaction = function (trFn, store_names, opt_mode, oncompleted, opt_args) {
//console.log('tr starting ' + trFn.name);
var scope_name = trFn.name || '';
var names = store_names;
if (goog.isString(store_names)) {
names = [store_names];
} else if (!goog.isArray(store_names) ||
(store_names.length > 0 && !goog.isString(store_names[0]))) {
throw new ydn.error.ArgumentException("storeNames");
}
var mode = goog.isDef(opt_mode) ? opt_mode : ydn.db.TransactionMode.READ_ONLY;
var outFn = trFn;
if (arguments.length > 4) { // handle optional parameters
var args = Array.prototype.slice.call(arguments, 4);
outFn = function () {
// Prepend the bound arguments to the current arguments.
var newArgs = Array.prototype.slice.call(arguments);
//newArgs.unshift.apply(newArgs, args); // pre-apply
newArgs = newArgs.concat(args); // post-apply
return trFn.apply(this, newArgs);
}
}
outFn.name = scope_name;
var me = this;
if (this.mu_tx_.isActive()) {
//console.log(this + ' active')
this.pushTxQueue(outFn, store_names, mode, oncompleted);
} else {
//console.log(this + ' not active')
var transaction_process = function (tx) {
me.mu_tx_.up(tx, scope_name);
// now execute transaction process
outFn(me);
me.mu_tx_.out(); // flag transaction callback scope is over.
// transaction is still active and use in followup request handlers
};
var completed_handler = function (type, event) {
me.mu_tx_.down(type, event);
/**
* @preserve_try
*/
try {
if (goog.isFunction(oncompleted)) {
oncompleted(type, event);
}
} catch (e) {
// swallow error. document it publicly.
// this is necessary to continue transaction queue
if (goog.DEBUG) {
throw e;
}
} finally {
me.popTxQueue_();
}
};
if (ydn.db.tr.TxStorage.DEBUG) {
window.console.log(this + ' transaction ' + mode + ' open for ' + JSON.stringify(names) + ' in ' + scope_name);
}
this.storage_.newTransaction(transaction_process, names, mode, completed_handler);
}
};
答案 1 :(得分:0)
事实证明,在websql导致问题之前初始化Facebook。在评论FB后,应用程序运行正常,这就是setTimeout也解决了这个问题的原因; fb api准备好了。我不知道如何阻止执行的线程。
所以,对任何使用FB然后尝试执行websql事务的人来说......延迟FB!
虽然,websql在safari加载上的运行速度仍然有点......