简体   繁体   中英

IndexedDB: `DOMException` while opening an `IDBObjectStore`, that the transaction has finished

When trying to open IDBObjectStore ( ObjectStore in dart):

Error message

(exact copy-paste)

[+5324 ms] DOMException: Failed to execute 'objectStore' on 'IDBTransaction': The transaction has finished.
                    dart-sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart 1233:22                   objectStore]
                    packages/scholar/scholar/client/flutter/dart/src/main.dart 21:35                  <fn>
                    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
                    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
                    dart-sdk/lib/html/dart2js/html_dart2js.dart 37317:58                              <fn>

Error causing function

Future<db.ObjectStore> completeTransaction(
  final db.Transaction transaction,
  final String objectStoreName,
) {
  final Completer<db.ObjectStore> completer = Completer<db.ObjectStore>.sync();

  transaction.onError.listen(completer.completeError);

  transaction.onComplete.listen((final html.Event e) {
    completer.complete((e.target as db.Transaction).objectStore(objectStoreName));
    //completer.complete(transaction.objectStore(objectStoreName)); /// also tried this
  });

  return completer.future;
}

Accessed through

void main() async {
  ...

    await completeTransaction(
      database.transactionStore(objectStoreName, "readwrite" /** also tried "readonly" */),
      objectStoreName,
    );

  ...
}

Source of the problematic function

It was forked from a core library, function, which handles db.Request (not db.Transaction ) types,

Future<T> _completeRequest<T>(db.Request request) {
  var completer = new Completer<T>.sync();
  
  request.onSuccess.listen((e) {
    T result = request.result;
    completer.complete(result);
  });
  
  request.onError.listen(completer.completeError);
  
  return completer.future;
}

Thanking you,

This happends, when the value of Database , ObjectStore , or Transaction , is stored in some variable, and used later, instead of getting a fresh value, using library functions,

hence, the result or value of the above mentioned types, should not be stored(or cached), in variables, but need to be recalled/opened each time, they are used

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM