Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexeddb: Differences between onsuccess and oncomplete?

I use two different events for the callback to respond when the IndexedDB transaction finishes or is successful:

Let's say... db : IDBDatabase object, tr : IDBTransaction object, os : IDBObjectStore object

tr = db.transaction(os_name,'readwrite');
os = tr.objectStore();

case 1 :

r = os.openCursor();
r.onsuccess = function(){
    if(r.result){
        callback_for_result_fetched();
        r.result.continue;
    }else callback_for_transaction_finish();
}

case 2:

tr.oncomplete = callback_for_transaction_finish();

It is a waste if both of them work similarly. So can you tell me, is there any difference between them?

like image 845
stack underflow Avatar asked Jun 22 '12 02:06

stack underflow


People also ask

What is IndexedDB?

IndexedDB is a key-value database in the browser. It is a NoSQL Database. It is transactional, i.e. if a particular action falls within a transaction, none of the actions of that transaction is applied. This ensures the database remains consistent. Why use IndexedDB?

How many object stores should an IndexedDB database have?

For example, for a site persisting user profiles and notes, you can imagine a people object store containing person objects, and a notes object store. A well structured IndexedDB database should have one object store for each type of data that needs to be persisted.

What is IndexedDB callback function?

With IndexedDB, every operation or transactionon our database must occur within a callback function. To do that, our database object needs to be available to every function that makes a transaction.

What is the difference between IndexedDB promised and API?

The IndexedDB Promised library sits on top of the IndexedDB API, translating its requests into promises. The overall structure is the same between the library and the API and, in general, the actual syntax for the database operations is the same and they will act the same way.


2 Answers

Sorry for raising up quite an old thread, but it's questioning is a good starting point...

I've looked for a similar question but in a bit different use case and actually found no good answers or even a misleading ones.

Think of a use case when you need to make several writes into the objectStore of even into several ones. You definitely don't want to manage each single write and it's own success and error events. That is the meaning of transaction and this is the (proper) implementation of it for indexedDB:

var trx = dbInstance.transaction([storeIdA, storeIdB], 'readwrite'),
    storeA = trx.objectStore(storeIdA),
    storeB = trx.objectStore(storeIdB);

    trx.oncomplete = function(event) {
        // this code will run only when ALL of the following requests are succeed
        // and only AFTER ALL of them were processed
    };
    trx.onerror = function(error) {
        // this code will run if ANY of the following requests will fail
        // and only AFTER ALL of them were processed
    };

    storeA.put({ key:keyA, value:valueA });
    storeA.put({ key:keyB, value:valueB });
    storeB.put({ key:keyA, value:valueA });
    storeB.put({ key:keyB, value:valueB });

Clue to this understanding is to be found in the following statement of W3C spec:

To determine if a transaction has completed successfully, listen to the transaction’s complete event rather than the success event of a particular request, because the transaction may still fail after the success event fires.

like image 115
GullerYA Avatar answered Oct 03 '22 08:10

GullerYA


While it's true these callbacks function similarly they are not the same: the difference between onsuccess and oncomplete is that transactions complete but requests, which are made on those transactions, are successful.

oncomplete is only defined in the spec as related to a transaction. A transaction doesn't have an onsuccess callback.

like image 31
buley Avatar answered Oct 03 '22 09:10

buley