从JobIntentService使用Firestore:无法获得对Firestore客户端的脱机持久性的独占锁定 [英] Using Firestore from a JobIntentService: Failed to gain exclusive lock to the Firestore client's offline persistence

查看:105
本文介绍了从JobIntentService使用Firestore:无法获得对Firestore客户端的脱机持久性的独占锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我设置了警报并退出应用程序,并且在应用程序处于"DEAD"状态时警报响起时,尝试更新Firestore中的字段时都会收到异常消息.

Whenever I exit the app while I have an alarm set and the alarm goes off while the app is "DEAD" I get an Exception while trying to update a field in Firestore.

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

The code works when the app is running in the foreground so I really have no clue of what is going on. Either way, here is the code for 2 functions which get called from the JobIntentService which is in turn created from a 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客户端的离线持久性的独占锁定.

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

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

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. (错误的设计,我知道...)

I'm happy to announce that I found a solution! I was using two consequently firing JobIntentServices - one for completedSessions, the other for sessionProgress. (Bad design, I know...)

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

When I played around with it and made just ONE JobIntentService to call both of these functions, the exception is gone which makes perfect sense.

这篇关于从JobIntentService使用Firestore:无法获得对Firestore客户端的脱机持久性的独占锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆