【问题标题】:Using Firestore from a JobIntentService: Failed to gain exclusive lock to the Firestore client's offline persistence从 JobIntentService 使用 Firestore:无法获得对 Firestore 客户端脱机持久性的独占锁定
【发布时间】:2018-02-25 13:08:42
【问题描述】:

每当我在设置了闹钟时退出应用程序,并且在应用程序处于“死亡”状态时闹钟响起时,我在尝试更新 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))。

我将不胜感激。谢谢!

【问题讨论】:

    标签: firebase kotlin background-process google-cloud-firestore jobintentservice


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 2021-08-23
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多