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

帅比萌擦擦* 提交于 2019-12-13 04:00:02

问题


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.

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))
        }
    }

The full error goes as follows:

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.

I will appreciate any help. Thank you!


回答1:


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...)

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.



来源:https://stackoverflow.com/questions/48973787/using-firestore-from-a-jobintentservice-failed-to-gain-exclusive-lock-to-the-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!