【问题标题】:FirebaseFirestore multiple processesFirebaseFirestore 多进程
【发布时间】:2018-03-16 16:59:17
【问题描述】:

我有一个带有 Firestore 的应用程序。我有很多存储库。他们在 Firestore 工作。当我同时调用 2 个方法时,我得到了一个错误。

class CommentRepository : CommentRepositoryInterface {

    val firebaseFirestore = FirebaseFirestore.getInstance()


    companion object {
        const val COLLECTION_NAME = "post_comments"
        const val COMMENT_POST_ID_KEY = "postid"
    }

    override fun getPostCommentsById(postId: String): Observable<CommentModel> {

        return Observable.create { subscriber ->

            firebaseFirestore.collection(COLLECTION_NAME)
                    .whereEqualTo(COMMENT_POST_ID_KEY, postId)
                    .get()
                    .addOnCompleteListener { task ->

                        if (task.isSuccessful) {
                            for (document in task.result) {
                                if (document.exists()) {
                                    val documentModel = document.toObject(CommentModel::class.java)
                                    subscriber.onNext(documentModel)
                                }
                            }
                            subscriber.onComplete()
                        } else {
                            subscriber.onError(task.exception!!) // TODO
                        }
                    }
        }
    }
}

另一个几乎是一样的,但那个正在使用另一个集合。 所以当我调用这些函数时,我得到了下一个错误:

Internal error in Firestore (0.6.6-dev).
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.

在 MyApplication 类中,我尝试设置 Singleton 的 Firestore 设置。

val settings = FirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(true)
                .build()
        FirebaseFirestore.getInstance().firestoreSettings = settings

我在 Firestore 的文档中找到它:

对于 Android 和 iOS,默认启用离线持久化。

有人有解决这个问题的想法吗?

【问题讨论】:

标签: android firebase google-cloud-firestore


【解决方案1】:

我已经清除了应用的缓存,问题解决了。

这样做或只是从手机中删除! :)

【讨论】:

  • 每次我更改 '.setPersistenceEnabled(true/false)' 时,我都会清除缓存,并修复该错误
  • 这就是答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2021-02-24
  • 2019-02-20
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
相关资源
最近更新 更多