简体   繁体   English

如何检查FirebaseFirestore是否已在Android中启动?

[英]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)? 一个简单的问题:如何内联检查Firestore是否已在Android(Java和Kotlin)中启动?

Unnecessary explanation why I want to know: 不必要的解释为什么我想知道:

I have a few different activities using Firestore. 我使用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: 如果我尝试在其他活动中进行设置后再设置(例如PersistenceEnabledCacheSize ),它将使应用程序崩溃并返回以下内容:

"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." “错误:FirebaseFirestore已经启动,并且其设置无法再更改。您只能在调用FirebaseFirestore对象上的任何其他方法之前调用setFirestoreSettings()。”

I understand I can simply set the settings in the first/main/splash activity within the app. 我了解我可以在应用程序的first / main / splash活动中简单地设置设置。 That will remove the immediate problem, but not in specific use cases that won't follow the normal UI flow. 这将消除紧迫的问题,但在不遵循常规UI流程的特定用例中不会解决。

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. 例如,一个深层链接打开了活动B,然后用户备份到活动A(最初应用设置),然后该应用将崩溃,因为当活动B使用Firestore并且活动A尝试再次设置它们时,应用了默认设置。

A simple inline check in activityA would allow me to apply custom settings without crashing the app. 一个简单的inlineA活动内联检查将允许我应用自定义设置,而不会导致应用程序崩溃。 (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.) (是的,我可以将应用程序设计为始终执行ActivityA并根据意图加载正确的活动,但是这样做效率不高,并且会产生从深层链接打开的时间/性能开销。)

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. 您可以通过另一个单例或静态方法强制所有对FirebaseFirestore单例的访问,该方法仅在调用后一次执行初始化。 A subsequent access using this method would have to check internally if it already initialized it, then skip the init. 使用此方法的后续访问将必须在内部检查其是否已初始化,然后跳过init。

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

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