iOS 更新后 IndexedDB 游标未获取下一条记录
IndexedDB cursor does not get next record after update on iOS
我曾尝试研究类似的案例,但没有什么能与我所经历的相符。考虑以下因素:
var req = indexedDB.open("myDatabase", 1);
req.onupgradeneeded = function (evt) {
var objStore = evt.target.result.createObjectStore("customers", { keyPath: "ID", autoIncrement: true });
objStore.createIndex("fName", "fName", { unique: false });
objStore.createIndex("lName", "lName", { unique: false });
objStore.createIndex("country", "country", { unique: false });
var obj1 = { fName: "James", lName: "Smith", age: 25, country: "United States", reviewStatus: "pending" };
var obj2 = { fName: "Tim", lName: "Jones", age: 32, country: "Canada", reviewStatus: "pending"};
var obj3 = { fName: "James", lName: "Miller", age: 22, country: "United States", reviewStatus: "pending"};
var obj4 = { fName: "James", lName: "Thompson", age: 40, country: "United Kingdom", reviewStatus: "pending"};
var obj5 = { fName: "Joseph", lName: "Harris", age: 28, country: "Canada", reviewStatus: "pending"};
var obj6 = { fName: "James", lName: "Jackson", age: 30, country: "United States", reviewStatus: "pending"};
objStore.add(obj1);
objStore.add(obj2);
objStore.add(obj3);
objStore.add(obj4);
objStore.add(obj5);
objStore.add(obj6);
};
req.onsuccess = function (evt) {
var tx = this.result.transaction("customers", "readwrite");
var objStore = tx.objectStore("customers");
var index = objStore.index("fName");
var cnt = index.count(IDBKeyRange.only("James"));
cnt.onsuccess = function () {
console.log(cnt.result + " records with fName: James");
};
var cursor = index.openCursor(IDBKeyRange.only("James"));
cursor.onsuccess = function (e) {
var record = e.target.result;
if (record) {
var updateData = record.value;
updateData.reviewStatus = "reviewed";
//update some other fields
var updateReq = record.update(updateData);
console.log("updating record");
record.continue();
}
};
cursor.onerror = function () {
console.log("error using cursor");
};
tx.oncomplete = function () {
console.log("got to transaction oncomplete");
//do other work required once cursor updates done
};
tx.onerror = function () {
console.log("error in transaction");
};
};
req.onerror = function () {
console.log("error opening database");
};
我的数据库有 4 条 fName = "James" 的记录。我使用 Chrome (v71) 首先测试这个并得到结果:
4 records with fName: James
updating record
updating
record
updating record
updating record
got to
transaction oncomplete
针对 Android 在 Firefox (v64)、Opera (v56) 和 Chrome 上进行的后续测试都产生了这些预期结果。当我在 iOS (11.4) 上测试时,我得到了结果:
4 records with fName: James
updating record
got to
transaction oncomplete
我没有收到任何控制台错误或我的 onerror 函数中的任何消息。我不明白为什么在 iOS 中只有第一条记录被更新而光标没有移动到其他记录。我的代码中是否存在我不知道的错误,或者这是 iOS 中的某种错误?
看起来这是 Safari 中的一个错误。我在这里提交了错误报告 https://bugs.webkit.org/show_bug.cgi?id=192987 - 希望他们能在不久的将来修复它!
我曾尝试研究类似的案例,但没有什么能与我所经历的相符。考虑以下因素:
var req = indexedDB.open("myDatabase", 1);
req.onupgradeneeded = function (evt) {
var objStore = evt.target.result.createObjectStore("customers", { keyPath: "ID", autoIncrement: true });
objStore.createIndex("fName", "fName", { unique: false });
objStore.createIndex("lName", "lName", { unique: false });
objStore.createIndex("country", "country", { unique: false });
var obj1 = { fName: "James", lName: "Smith", age: 25, country: "United States", reviewStatus: "pending" };
var obj2 = { fName: "Tim", lName: "Jones", age: 32, country: "Canada", reviewStatus: "pending"};
var obj3 = { fName: "James", lName: "Miller", age: 22, country: "United States", reviewStatus: "pending"};
var obj4 = { fName: "James", lName: "Thompson", age: 40, country: "United Kingdom", reviewStatus: "pending"};
var obj5 = { fName: "Joseph", lName: "Harris", age: 28, country: "Canada", reviewStatus: "pending"};
var obj6 = { fName: "James", lName: "Jackson", age: 30, country: "United States", reviewStatus: "pending"};
objStore.add(obj1);
objStore.add(obj2);
objStore.add(obj3);
objStore.add(obj4);
objStore.add(obj5);
objStore.add(obj6);
};
req.onsuccess = function (evt) {
var tx = this.result.transaction("customers", "readwrite");
var objStore = tx.objectStore("customers");
var index = objStore.index("fName");
var cnt = index.count(IDBKeyRange.only("James"));
cnt.onsuccess = function () {
console.log(cnt.result + " records with fName: James");
};
var cursor = index.openCursor(IDBKeyRange.only("James"));
cursor.onsuccess = function (e) {
var record = e.target.result;
if (record) {
var updateData = record.value;
updateData.reviewStatus = "reviewed";
//update some other fields
var updateReq = record.update(updateData);
console.log("updating record");
record.continue();
}
};
cursor.onerror = function () {
console.log("error using cursor");
};
tx.oncomplete = function () {
console.log("got to transaction oncomplete");
//do other work required once cursor updates done
};
tx.onerror = function () {
console.log("error in transaction");
};
};
req.onerror = function () {
console.log("error opening database");
};
我的数据库有 4 条 fName = "James" 的记录。我使用 Chrome (v71) 首先测试这个并得到结果:
4 records with fName: James
updating record
updating record
updating record
updating record
got to transaction oncomplete
针对 Android 在 Firefox (v64)、Opera (v56) 和 Chrome 上进行的后续测试都产生了这些预期结果。当我在 iOS (11.4) 上测试时,我得到了结果:
4 records with fName: James
updating record
got to transaction oncomplete
我没有收到任何控制台错误或我的 onerror 函数中的任何消息。我不明白为什么在 iOS 中只有第一条记录被更新而光标没有移动到其他记录。我的代码中是否存在我不知道的错误,或者这是 iOS 中的某种错误?
看起来这是 Safari 中的一个错误。我在这里提交了错误报告 https://bugs.webkit.org/show_bug.cgi?id=192987 - 希望他们能在不久的将来修复它!