I have an IndexedDB data store with a few hundred objects in it. I'd like to grab items 40-59 from it based on the ordering in one of my indexes on that store. Is there a way to do that without simply calling cursor.continue() 39 times before starting to consume data? It seems pretty wasteful in terms of processing time.
The IDBCursor interface of the IndexedDB API represents a cursor for traversing or iterating over multiple records in a database. The cursor has a source that indicates which index or object store it is iterating over.
Not slow like a database on a cheap server, even slower! Inserting a few hundred documents can take up several seconds. Time which can be critical for a fast page load.
Operations performed using IndexedDB are done asynchronously, so as not to block applications.
I had the same problem and cursor.advance(40)
is what you want to use.
One thing that took me a while to figure out so may be useful to others is if you want to advance the cursor and iterate through the results you will either need to call them in separate openCursor().onsuccess
handlers, or implement some kind of tracking to prevent them both being called in the same request or an InvalidStateError
exception with be thrown. This can be done like so:
Separate Handlers
// advance first
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.advance(40);
};
// then iterate
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.continue();
});
Same Handler
// create flag for advancing
var advancing = true;
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
// advancing
if (advancing === true) {
cursor.advance(40);
// set advancing flag to false so we don't advance again
advancing = false;
}
// continuing
else {
cursor.continue();
}
}
Spec reference: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance-void-unsigned-long-count MDN Reference with example: https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor.advance
I believe you can call cursor.advance(40)
Spec reference: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With