简体   繁体   中英

FirebaseFirestore multiple processes

I have an App with Firestore. I have a lot of Repositories. They work from Firestore. When I call 2 method in same time then I got an error.

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

The another one is almost same like that, but that one is using another collection. So when I called these functions, then I got the next error:

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.

In the MyApplication class I tried to set the Singleton's of firestore settings.

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

I found it in Firestore's Doc:

For Android and iOS, offline persistence is enabled by default.

Anyone have idea to solve this problem?

I've cleared the App's Caching and the problem solved.

Do it or just remove from the phone! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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