Primary Key issue on iOS8 implementation of IndexedDb

前端 未结 2 1817
心在旅途
心在旅途 2020-12-08 00:37

The issue is when you have two different object stores in the same indexeddb, primary key values appear to be \"shared\" across all stores.


             


        
2条回答
  •  悲&欢浪女
    2020-12-08 01:21

    I can confirm that iOS8 is definitely buggy here. I tried a few workarounds, but the best I can suggest is a primary key that combines some unique string, like the name of the objectStore, with a number. So for example, given two objectStores called people and notes, I'd store data with keys like so:

    people/X notes/X

    You can set X manually, or, use the .count() method on the objectStore to find the count and add one. Here is an example:

    //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");
        }
    
    }
    

    Note that I specified keyPath of "id" for this OS.

提交回复
热议问题