繁体   English   中英

FirebaseFirestore多个进程

[英]FirebaseFirestore multiple processes

我有一个带有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类中,我尝试设置Fireton设置的Singleton。

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

我在Firestore的文档中找到了它:

对于Android和iOS,默认情况下启用离线持久性。

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

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

做到这一点或只是从手机中删除! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM