0

每当我在设置警报时退出应用程序并且在应用程序“死亡”时警报响起时,我在尝试更新 Firestore 中的字段时遇到异常。

该代码在应用程序在前台运行时有效,所以我真的不知道发生了什么。无论哪种方式,这是从 JobIntentService 调用的 2 个函数的代码,而 JobIntentService 又从 BroadcastReceiver 创建:

private val firestoreInstance: FirebaseFirestore by lazy { FirebaseFirestore.getInstance() }

    fun updateTaskCompletedSessions(taskDocRefPath: String, completedSessions: Int){
        val taskDocRef = firestoreInstance.document(taskDocRefPath)
        taskDocRef.get().addOnSuccessListener { documentSnapshot ->
            documentSnapshot.reference
                    .update(mapOf("completedSessions" to completedSessions))
        }
    }

    fun updateTaskSessionProgress(taskDocRefPath: String, sessionProgress: String){
        val taskDocRef = firestoreInstance.document(taskDocRefPath)
        taskDocRef.get().addOnSuccessListener { documentSnapshot ->
            documentSnapshot.reference
                    .update(mapOf("sessionProgress" to sessionProgress))
        }
    }

完整的错误如下:

未能获得 Firestore 客户端离线持久性的独占锁定。

这通常意味着您在应用中的多个进程中使用 Firestore。请记住,多进程 Android 应用程序Application会在所有进程中执行您的类中的代码,因此您可能需要避免在您的Application类中初始化 Firestore。如果您有意从多个进程中使用 Firestore,则只能setPersistenceEnabled(true)在其中一个进程中启用离线持久性(即 call )。

我将不胜感激。谢谢!

4

1 回答 1

0

我很高兴地宣布我找到了解决方案!我使用了两个因此触发 JobIntentServices - 一个用于completedSessions,另一个用于sessionProgress。(糟糕的设计,我知道......)

当我使用它并只使用一个 JobIntentService 来调用这两个函数时,异常消失了,这很有意义。

于 2018-02-26T18:18:13.943 回答