繁体   English   中英

如何在整个 android 应用程序中使用单个 Firestore 实例?

[英]How to use a single Firestore instance throughout an android app?

我正在尝试在整个应用程序中使用单个 Firestore 实例,因为使用 Firebase 的冷启动,第一个查询或要获取的第一个数据需要一些时间(例如 2000 毫秒-3000 毫秒)。 I tried having Firestore object as static in a class that extends Application but it shows a Lint warning "Do not place Android context classes in static fields; this is a memory leak". 我不知道 go 从这里到哪里。 请帮我。

getInstance()将在您使用时为您提供整个应用程序中的唯一实例

FirebaseFirestore.getInstance()

或者

FirebaseDatabase.getInstance()

如果您在不同的类中创建它们,它不会创建另一个实例,而是会一遍又一遍地重用同一个实例。

您还可以通过ctrl + click IDE 中的方法查看实现

@NonNull
  public static FirebaseFirestore getInstance() {
    FirebaseApp app = FirebaseApp.getInstance();
    if (app == null) {
      throw new IllegalStateException("You must call FirebaseApp.initializeApp first.");
    } else {
      return getInstance(app, "(default)");
    }
  }




@NonNull
  public static FirebaseApp getInstance() {
    synchronized(LOCK) {
      FirebaseApp defaultApp = (FirebaseApp)INSTANCES.get("[DEFAULT]");
      if (defaultApp == null) {
        throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + ProcessUtils.getMyProcessName() + ". Make sure to call FirebaseApp.initializeApp(Context) first.");
      } else {
        return defaultApp;
      }
    }
  }

暂无
暂无

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

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