从 JobIntentService 使用 Firestore:无法获得对 Firestore 客户端离线持久性的独占锁定

Using Firestore from a JobIntentService: Failed to gain exclusive lock to the Firestore client's offline persistence

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

完整错误如下:

Failed to gain exclusive lock to the Firestore client's offline persistence.

This generally means you are using Firestore from multiple processes in your app. Keep in mind that multi-process Android apps execute the code in your Application class in all processes, so you may need to avoid initializing Firestore in your Application class. If you are intentionally using Firestore from multiple processes, you can only enable offline persistence (i.e. call setPersistenceEnabled(true)) in one of them.

如有任何帮助,我将不胜感激。谢谢!

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

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