我有一个应用程序,我将序列化的图像存储到 websql 中。我首先从 xml 获取图像列表,创建一个路径数组,这些路径指向返回 jsonp 的单个文件(单个文件,以便我可以有一个进度指示器)。在我得到响应后,我将序列化图像传递给我的数据库函数,然后执行插入,成功更新图像路径数组,然后启动下一个 ajax 请求。这在我的本地机器上运行良好。在 iOS(模拟器和设备)中,发生的事情是在某个时刻 - 比如说在第 300 张左右的图像左右,我所有的 WebSQL 事务都失败了。他们这样做没有任何错误代码或文本,所以我不知道那里发生了什么。它是失败的事务,而不是executeSql。
序列化数据的长度平均约为 45k,有些可达 6 位。txn 上没有失败的模式 - 字符串长度是随机的 - 唯一一致的是,在数百次成功的 txn 之后,某个点之后的一切都失败了,剩余的 web sql 事务(当时大约 157 个)。
这个过程非常线性:通过 ajax 获取数据 - 检查它(时间戳等) - 路由到插入函数,然后重复。
我计算机上的 SQL lite db 的大小约为 35mb,所以我认为我没有达到设备的上限。
这包含在 Cordova2.2 中。jQuery 用于 Ajax 请求(如您所见)。在我的带有 Ripple 的最新 chrome 的 Windows 机器上,我完全没有错误 - 所有图像都下载。
代码示例:
var _SQL_DB = null;
var _SQL_DB_SETTINGS = {name:'myapp',version:'1.0',displayname:'myapp db',size:(50 * 1024 * 1024)};
// much code removed, here's the database init:
function initWebSQL(){
_SQL_DB = window.openDatabase(_SQL_DB_SETTINGS.name, _SQL_DB_SETTINGS.version, _SQL_DB_SETTINGS.displayname, _SQL_DB_SETTINGS.size);
_SQL_DB.transaction(
function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS IMAGES (ID INTEGER PRIMARY KEY ASC, FNAME TEXT, FDATE TEXT, DATASTR TEXT)');},
function(){ // error CB
app.util.printToConsole('IMAGES table was not created');
},
function(){ // success CB
app.util.printToConsole('IMAGES table was created succesfully');
}
);
}
// ... fast forward through more code....
// ajax request
function _startImgCaching(){
$.ajax({
url: app.host() + path,
type:'GET',
dataType:'JSONP',
timeout: 10000,
error:function(xhr,status,err){
// error logic removed
},
success:function(responseData,status,xhr){
// save data
_dbInsert(rawPath,_queue.images[0].date,responseData.data);
// check if more need to be downloaded and stored locally
if (_queue.length > 0){
_queue.images.splice(0,1);
app.util.updateProgress(_queue.length,_queue.images.length);
setTimeout(_cacheImages,200);
}
});
// verify the data, check timestamps...
function _cacheImages(){
// removed logic that checks for duplicates and routes to delete/insert methods as needed
}
// do the insert
function _dbInsert(imgName,imgDate,imgData){
_SQL_DB.transaction(
function(tx){
tx.executeSql('INSERT INTO IMAGES(FNAME, FDATE, DATASTR) VALUES (?,?,?)',
[imgName,imgDate,imgData],
_insertSuccess,
_insertFailure
);
},_transactionFailure,_transactionSuccess
);
}
// the txn callbacks
function _transactionSuccess(tx,r){
app.util.printToConsole('txn successful');
if (r){
app.util.printToConsole(r);
}
}
function _transactionFailure(tx,e){
// e in this case is always undefined
app.util.printToConsole('txn failed - ' + (e ? e.message : 'no err msg available'));
}