简体   繁体   中英

How to check if FirebaseFirestore has already been started in Android?

Simple Question: How to inline check if Firestore has already been started or not in Android (Java and Kotlin)?

Unnecessary explanation why I want to know:

I have a few different activities using Firestore. If I try to set the settings (such as PersistenceEnabled or CacheSize ) after it has been set in another activity it will crash the app and return the following:

"error:FirebaseFirestore has already been started and its settings can no longer be changed. You can only call setFirestoreSettings() before calling any other methods on a FirebaseFirestore object."

I understand I can simply set the settings in the first/main/splash activity within the app. That will remove the immediate problem, but not in specific use cases that won't follow the normal UI flow.

For instance a deep link that opens activityB and then the user backs up to activityA (where the settings are initially applied) and then the app would crash because the default settings were applied when activityB used Firestore and activityA tried to set them again.

A simple inline check in activityA would allow me to apply custom settings without crashing the app. (Yes, I could design the app to always go through activityA and load the correct activity depending on the intent, but it would be inefficient and create a slight time/performance cost of opening from a deep link.)

You could force all your access to the FirebaseFirestore singleton through another singleton or static method that only performs the initialization a single time after it's invoked. A subsequent access using this method would have to check internally if it already initialized it, then skip the init.

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