gpt4 book ai didi

javascript - IndexedDb iOS8 实现的主键问题

转载 作者:IT王子 更新时间:2023-10-29 03:22:21 25 4
gpt4 key购买 nike

问题是当您在同一个 indexeddb 中有两个不同的对象存储时,主键值似乎在所有存储中“共享”。

<body>
<script type="text/javascript">
//prefixes of implementation that we want to test
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

//prefixes of window.IDB objects
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange

if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}


var db;
var request = window.indexedDB.open("newDatabase", 4);

request.onerror = function(event) {
console.log("error: ");
};

request.onsuccess = function(event) {
db = request.result;
console.log("success: "+ db);
};

request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("customers", {keyPath: "arseid"});
var objectStore = db.createObjectStore("test", {keyPath: "id"});
}



function add1() {
var x = new Date();
var h1 = x.getHours();
var m1 = x.getMinutes();
var s1 = x.getSeconds();
console.log('starting insert on ' + h1 + ':' + m1 + ':' + s1);

var tx = db.transaction(["customers"], "readwrite");
for (var i = 0; i < 1000; i++) {
var request = tx.objectStore("customers")
.put({ arseid: i, name: "Jonathan Smith", email: "jonathan.smith@anemailaddress.com", favourite: "chocolate cake", pet: "rudolph the red nose reindeer", address: "999 letsbe avenue, townton, countyshire" });
}


tx.oncomplete = function (e) {
// Re-render all the todo's
var x2 = new Date();
var h2 = x2.getHours();
var m2 = x2.getMinutes();
var s2 = x2.getSeconds();
console.log('transaction complete ' + h2 + ':' + m2 + ':' + s2);
}
}


function add2() {
//tx 2
var tx2 = db.transaction(["test"], "readwrite");
for (var i = 0; i < 1000; i++) {
var request2 = tx2.objectStore("test")
.put({ id: i, name: "Robwin Mwengway", email: "jonathan.smith@anemailaddress.com", favourite: "chocolate cake", pet: "rudolph the red nose reindeer", address: "999 letsbe avenue, townton, countyshire" });
}

tx2.oncomplete = function (e) {
var x3 = new Date();
var h3 = x3.getHours();
var m3 = x3.getMinutes();
var s3 = x3.getSeconds();
console.log('transaction complete ' + h3 + ':' + m3 + ':' + s3);
}
}


</script>
<button onclick="add1()">Add1 data to indexedDb</button>
<button onclick="add2()">Add2 data to indexedDb</button>
</body>

( fiddle :http://jsfiddle.net/jonnyknowsbest/4pdp8vxe/)

在 iOS8 中,如果您运行 fiddle 并单击“Add1 data to IndexedDb”,那么 1000 个条目将添加到“customers”表中。如果您随后单击“向 IndexedDb 添加 2 个数据”,则会将 1000 个条目添加到“vendor ”表中,但从“客户”表中删除 1000 个条目。

有没有人遇到过这个?这是 IndexedDb 规范的一部分吗? Chrome 好像没有这个问题。

编辑:找到这个 W3 Org IndexedDB Recommendation :“给定的对象存储中永远不会有多个具有相同键的记录。” Apple 似乎已经在数据库级别应用了这一点。

最佳答案

我可以确认 iOS8 在这里肯定是有问题的。我尝试了一些变通方法,但我能建议的最好方法是将一些唯一字符串(如 objectStore 的名称)与数字组合在一起的主键。因此,例如,给定两个名为 people 和 notes 的 objectStores,我将使用如下键存储数据:

人/X备注/X

您可以手动设置 X,或者使用 objectStore 上的 .count() 方法来查找计数并加一。这是一个例子:

//Define a person
var person = {
name:"Ray",
created:new Date().toString(),
}

//Perform the add
db.transaction(["people"],"readwrite").objectStore("people").count().onsuccess = function(event) {
var total = event.target.result;
console.log(total);
person.id = "person/" + (total+1);

var request = db.transaction(["people"],"readwrite").objectStore("people").add(person);

request.onerror = function(e) {
console.log("Error",e.target.error.name);
//some type of error handler
}

request.onsuccess = function(e) {
console.log("Woot! Did it");
}

}

请注意,我为此操作系统指定了“id”的 keyPath。

关于javascript - IndexedDb iOS8 实现的主键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26019147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com