bramski / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
165 stars 49 forks source link

'IDBObjectStore': The transaction has finished. #12

Closed Lunatic83 closed 9 years ago

Lunatic83 commented 9 years ago

Hi bramski, I'm opening another issue, because I tryed to develope another way to solve the issue #11, using Restangular, I would fetch my api entity if is not stored inside indexedDB store.

$indexedDB.openStore(entityName, function(table) { var d = $q.defer(); var promise = d.promise;

table.find(id).then(function(success) { //Change if id is used with index
    return d.resolve(success);
}, function(error) {
    Restangular.one(entityName, id).get().then(function(result) {
        table.insert(result, function(result) {
            console.log('insert ok');
        });
    });
    return d.reject(error);
});

return promise;

}, 'readwrite');

I have Error: Failed to execute 'add' on 'IDBObjectStore': The transaction has finished.

I understand the reason for async callback for restangular refers to table after the transaction is closed, is there any way to keep the transaction open and manage it inside restangular callback?

Thanks a lot for your support.

G

bramski commented 9 years ago

Hey @Lunatic83 , this is not really an issue with the library. The problem here is that you are opening an XHR within your transaction. That's not going to work given the async nature of javascript based promises. You also cannot both error and attempt to insert something in your transaction. If your transaction results in error, your transaction will be rolled back.

My recommended refactoring..(

var getItemAndAddToDb;

getItemAndAddToDb = function(entityName, id) {
  return Restangular.one(entityName, id).get().then(function(result) {
    return $indexedDB.openStore(entityName, function(store) {
      return store.insert(result);
    });
  });
};

$indexedDB.openStore(entityName, function(table) {
  return table.find(id).then(function(item) {
    return item;
  })["catch"](function(error) {
    return getItemAndAddToDb(entityName, id);
  });
});
bramski commented 9 years ago

I'm going to close this issue though as it's not really a bug persay. It would also be excellent if you could open & place your questions into stackoverflow and tag it "angular-indexed-db". That sort of method of first asking stack overflow questions seems most helpful to others who run into the same problems. Let me know when you open a question there and I am happy to direct you to a better solution.

lucd commented 9 years ago

Huhz

On Friday, November 21, 2014, Bram Whillock notifications@github.com wrote:

I'm going to close this issue though as it's not really a bug persay. It would also be excellent if you could open & place your questions into stackoverflow and tag it "angular-indexed-db". That sort of method of first asking stack overflow questions seems most helpful to others who run into the same problems. Let me know when you open a question there and I am happy to direct you to a better solution.

— Reply to this email directly or view it on GitHub https://github.com/bramski/angular-indexedDB/issues/12#issuecomment-64006007 .