Firestore数据库不断崩溃

时间:2017-11-01 13:51:39

标签: firebase firebase-realtime-database google-cloud-firestore

我已成功将我的应用程序从RealtimeDB迁移到Firestore但是在迁移应用程序后经常崩溃并出现以下错误,如何解决此问题?我在使用RealtimeDB

时从未遇到过这个错误
Fatal Exception: java.lang.RuntimeException: Internal error in Firestore (0.6.6-dev).

   at com.google.android.gms.internal.zzejs.run(Unknown Source)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6184)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)`

Caused by java.lang.RuntimeException: 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.

  `at com.google.android.gms.internal.zzefi.start(Unknown Source)
   at com.google.android.gms.internal.zzeca.zza(Unknown Source)
   at com.google.android.gms.internal.zzecc.run(Unknown Source)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
   at com.google.android.gms.internal.zzejp$zza.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:761)`

4 个答案:

答案 0 :(得分:1)

如果您尝试从多个线程访问数据库,则会出现此问题。因此,如果您使用RX,则需要为所有插入创建单个后台线程。这是因为Firestore在写入时会锁定数据库。

val FIRESTORE_OPERATION_THREAD = Schedulers.single()
yourSingle.subscribeOn(FIRESTORE_OPERATION_THREAD)
. ...

答案 1 :(得分:0)

当各种呼叫不相互交错(如重复插入)时,@Sandip Soni可以使用什么,以及Firebase的异步性质,他们很可能会这样做。对我来说有用的是一点点破解,同步对Firestore实例的访问并在任何外部线程之外触发一个写操作,以便让Firestore在它自己的线程中获取本地数据库实例。写操作是任意的,只是出于设置目的:

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

答案 2 :(得分:0)

您必须在应用程序的多个进程中使用Firestore。多进程Android应用在所有进程中都执行Application类中的代码。

要解决该错误,您可能需要:

  1. 避免在Application类或Rxjava模块中初始化Firestore。相反,在每次调用Firestore数据库之前,初始化新的Firestore实例,如下所示:

    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
    

    这意味着您不会对多个调用使用相同的Firestore实例。

  2. 然后清除您的应用程序缓存

答案 3 :(得分:-3)

只需清除设置中的Application's cache即可。这对我有用!