【问题标题】:Firestore transaction fatal exceptionFirestore 事务致命异常
【发布时间】:2020-05-19 20:26:31
【问题描述】:

大家好,我的颤振应用有问题。 我使用firebase事务来管理数据,我使用的版本是cloud_firestore: 0.13.5

我的应用程序实例在执行事务时崩溃。我调查了这个问题,但我找不到是什么原因造成的,以前的代码可以正常工作,没有任何问题。 这是我的堆栈跟踪。

E/AndroidRuntime( 7246): FATAL EXCEPTION: AsyncTask #5
E/AndroidRuntime( 7246): Process: be.dezijwegel.blackbox, 
PID: 7246
E/AndroidRuntime( 7246): java.lang.RuntimeException: An error 
occurred while executing doInBackground()
E/AndroidRuntime( 7246):        at 
android.os.AsyncTask$3.done(AsyncTask.java:354)
E/AndroidRuntime( 7246):        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime( 7246):        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime( 7246):        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime( 7246):        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
E/AndroidRuntime( 7246):        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 7246):        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 7246):        at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime( 7246): Caused by: java.lang.AssertionError: INTERNAL ASSERTION FAILED: A transaction object cannot be used after its update callback has been invoked.
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.util.Assert.fail(com.google.firebase:firebase-firestore@@21.3.0:46)
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.util.Assert.hardAssert(com.google.firebase:firebase-firestore@@21.3.0:31)
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.core.Transaction.ensureCommitNotCalled(com.google.firebase:firebase-firestore@@21.3.0:246)
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.core.Transaction.lookup(com.google.firebase:firebase-firestore@@21.3.0:81)
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.Transaction.getAsync(com.google.firebase:firebase-firestore@@21.3.0:191)
E/AndroidRuntime( 7246):        at com.google.firebase.firestore.Transaction.get(com.google.firebase:firebase-firestore@@21.3.0:228)
E/AndroidRuntime( 7246):        at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$5.doInBackground(CloudFirestorePlugin.java:613)
E/AndroidRuntime( 7246):        at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$5.doInBackground(CloudFirestorePlugin.java:608)
E/AndroidRuntime( 7246):        at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/AndroidRuntime( 7246):        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime( 7246):        ... 4 more
D/OSTracker( 7246): OS Event: crash
I/Process ( 7246): Sending signal. PID: 7246 SIG: 9
Lost connection to device.

仅供参考,这是我们用来进行获取交易的代码,我看不到任何问题。

    static Future< Appinfo > getAppInfo() async
  {
    Appinfo appinfo;
    DocumentReference docRef = 
 Firestore.instance.collection("appinfo").document("appinfo");

    await Firestore.instance.runTransaction((Transaction transaction) async {
      /// Get the app info document
      DocumentSnapshot snap = await transaction.get(docRef);

      if (snap.exists)
      {
        if (snap.data['current_version'] != null && snap.data['current_version'] != "")
        {
          String msg = "";
          if (snap.data['login_message'] != null && snap.data['login_message'] != "")
          {
            msg = snap.data['login_message'];
          }

          appinfo = new Appinfo(snap.data['current_version'], msg);

        }


      }

    });
    return appinfo;
  }

【问题讨论】:

  • 为什么要使用事务来获取文档?仅读取文档时不存在争用风险。

标签: firebase flutter dart google-cloud-firestore transactions


【解决方案1】:

试着把下面这行放在await Firestore.instance.runTransaction((Transaction transaction) async {上面

 DocumentReference docRef = Firestore.instance.collection("appinfo").document("appinfo");

将此作为交易的最后一行

//to prevent firestore transaction error
 transaction.update(docRef, {})
 return Promise.resolve(true)

【讨论】:

  • 这会导致以下异常:
  • PlatformException(Error performing transaction, Every document read in a transaction must also be written., null)
  • 需要更新事务并返回Promise.resolve
  • 所以如果我理解正确,每当我阅读时,我也必须更新实例?所以这导致我每次阅读时都会进行写操作?
  • 不完全是。在事务中,当您打开它时,您必须关闭它,这样您就没有这样做。
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
相关资源
最近更新 更多