Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging with IndexedDB cursor

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.

like image 218
Ben Dilts Avatar asked Mar 02 '11 06:03

Ben Dilts


People also ask

What is cursor in IndexedDB?

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.

Is IndexedDB fast?

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.

Is IndexedDB asynchronous?

Operations performed using IndexedDB are done asynchronously, so as not to block applications.


2 Answers

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

like image 63
Pete Newnham Avatar answered Oct 27 '22 00:10

Pete Newnham


I believe you can call cursor.advance(40)

Spec reference: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance

like image 29
Johnathan Hebert Avatar answered Oct 27 '22 01:10

Johnathan Hebert