我正在开发一个基于cordova的多平台Web应用程序,使用sapui5框架v1.44和indexedDB来存储数据。该应用程序工作正常,直到上次ios更新10.3.1,现在在尝试写入时崩溃到索引数据库。我正在使用 put 方法来更新数据,并且我对应用程序进行了全新安装。我尝试写入indexedDB的代码框架是这样的:
writeToIDB: function (objStoreName, result, success, error) {
//Asynchronous function
var defer = Q.defer();
var res = [];
if (!!result && Array.isArray(result)) {
res = result;
} else if (!!result && result.hasOwnProperty("results") && Array.isArray(result.results)) {
res = result.results;
} else if (!!result && typeof result === 'object') {
res.push(result);
}
if (res.length >= 0) {
if (window.myDB) {
if (!window.myDB.objectStoreNames.contains(objStoreName)) {
console.log("ObjectStore for " + objStoreName + " doesn't exist");
if (error) {
error("ko")
} else {
defer.reject("ko");
}
} else {
var oTransaction = window.myDB.transaction([objStoreName], "readwrite");
var oDataStore = oTransaction.objectStore(objStoreName);
oTransaction.oncomplete = function (event) {
console.log("Transaction completed: database modification for " + objStoreName + " finished.");
if (success) {
success();
} else {
defer.resolve("ok");
}
};
oTransaction.onerror = function (event) {
console.log("Transaction for " + objStoreName + " not opened due to error. Check for duplicate items or missing properties!");
console.log(event.target.error);
if (error) {
error("ko")
} else {
defer.reject("ko");
}
};
var oRecord = {};
for (var i = 0; i < res.length; i++) {
oRecord = res[i];
oDataStore.put(oRecord);
}
}
} else {
this.createIDB().then(
function (resCreate) {
console.log("DB Created successfully");
if (!window.myDB.objectStoreNames.contains(objStoreName)) {
console.log("ObjectStore for " + objStoreName + " doesn't exist");
if (error) {
error("ko")
} else {
defer.reject("ko");
}
} else {
var oTransaction = window.myDB.transaction([objStoreName], "readwrite");
var oDataStore = oTransaction.objectStore(objStoreName);
oTransaction.oncomplete = function (event) {
console.log("Transaction completed: database modification for " + objStoreName + " finished.");
if (success) {
success();
} else {
defer.resolve("ok");
}
};
oTransaction.onerror = function (event) {
console.log("Transaction for " + objStoreName + " not opened due to error. Check for duplicate items or missing properties!");
console.log(event.target.error);
if (error) {
error("ko")
} else {
defer.reject("ko");
}
};
var oRecord = {};
for (var i = 0; i < res.length; i++) {
oRecord = res[i];
oDataStore.put(oRecord);
}
}
}.bind(this),
function (err) {
console.log("DB Creation failed");
if (error) {
error("ko")
} else {
defer.reject("ko");
}
}.bind(this)
);
}
} else {
if (error) {
error("ko")
} else {
defer.reject("ko");
}
}
if (typeof success === 'undefined' && typeof error === 'undefined') {
return defer.promise;
}
},
P.S.我省略了部分代码。 这在以前版本的 ios 上工作得很好,我想我已经安装了 10.2.1,现在它在调用 put 方法后崩溃了。我现在尝试将ios升级到10.3.2的测试版,但结果是一样的。其他人注意到这一点或知道如何解决这个问题吗? 谢谢 K
更新 我发现了问题:复杂的数据类型。由于 IndexedDB 支持保存和检索复杂的数据类型,因此我有一些属性,它们是我用来保存在一些对象存储中的数组或对象。这对我来说绝对是一个大问题,因为我能想到的唯一解决方法是对复杂的字段进行处理,但由于我处理大量数据,这会产生很大的性能问题。我希望ios开发团队能够尽快找到解决方案
最佳答案
您确定 res[] 数组中的每个键都是有效键吗?这里有一个已关闭的错误:
https://bugs.webkit.org/show_bug.cgi?id=170000
看起来如果你传入无效的 key 将会导致 webkit 崩溃。
此问题的修复可能会包含在 iOS 的下一个公开版本中。
要确定有效 key ,请参阅 W3.org 规范的这一部分:
3.1.3 Keys
In order to efficiently retrieve records stored in an indexed database, each record is organized according to its key. A value is said to be a valid key if it is one of the following ECMAScript [ECMA-262] types: Number primitive value, String primitive value, Date object, or Array object. An Array is only a valid key if every item in the array is defined and is a valid key (i.e. sparse arrays can not be valid keys) and if the Array doesn't directly or indirectly contain itself. Any non-numeric properties on an Array are ignored, and thus do not affect whether the Array is a valid key. If the value is of type Number, it is only a valid key if it is not NaN. If the value is of type Date it is only a valid key if its [[PrimitiveValue]] internal property, as defined by [ECMA-262], is not NaN. Conforming user agents must support all valid keys as keys.
这是从这里获取的:
关于基于 IOS 10.3.1 cordova 的应用程序在尝试写入 IndexedDB 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524860/