【发布时间】: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