Firestore database keeps crashing

后端 未结 4 1406
别那么骄傲
别那么骄傲 2020-12-30 17:25

I have successfully migrated my app from RealtimeDB to Firestore but after migrating the app crashes too often with the following error, how to fix this?. I have never run i

相关标签:
4条回答
  • 2020-12-30 18:06

    What @Sandip Soni would work whenever various calls don't interleave each other(like repeated inserts) and by the async nature of Firebase they're likely to do it. What did work for me was a little hack, synchronizing the access to the Firestore instance and firing one write operation outside any foreign thread in order to let Firestore get the local db instance in it own thread. the write operation is an arbitrary one, it was just for setup purpose:

    class Firestore {
        companion object {
           val instance: FirebaseFirestore by lazy {
               return@lazy synchronized(Firestore::class){
                   FirebaseFirestore.getInstance().apply { lock() }
               }
           }
    
           private fun FirebaseFirestore.lock() {
              collection("config").document("db").update("locked", true)
           }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 18:11

    If you try to access the db from multiple threads, this issue will occur. So if you are using RX, you need to create a single background thread for all insertions. This is because Firestore locks the DB while writing.

    val FIRESTORE_OPERATION_THREAD = Schedulers.single()
    yourSingle.subscribeOn(FIRESTORE_OPERATION_THREAD)
    . ...
    
    0 讨论(0)
  • 2020-12-30 18:18

    You must be using Firestore from multiple processes in your app. Multi-process Android apps execute the code in your Application class in all processes.

    To solve the error you may need to:

    1. Avoid initializing Firestore in your Application class or in a module for Rxjava. Instead, initialize a new Firestore instance before every call to the firestore database like this:

      val fireStore = FirebaseFirestore.getInstance()
      val settings = FirebaseFirestoreSettings.Builder()
              //offline persistence is enabled automatically in Android
              .setTimestampsInSnapshotsEnabled(true)
              .build()
      fireStore.firestoreSettings = settings
      //go ahead and call database using the Firestore instance
      

      This means that you will not use the same Firestore instance for multiple calls.

    2. Then clear your application cache.

    0 讨论(0)
  • 2020-12-30 18:25

    Just clear the Application's cache in the settings. It's work for me!

    0 讨论(0)
提交回复
热议问题