Firestore.getInstance():如何使用?
Firestore.getInstance(): how to use?
Firebase Firestore 提供了 getInstance()
方法,它打开一个套接字(只有一个,在应用程序执行的任何时候)并实例化 Firestore 客户端。
我想查询和存储数据,使用或不使用侦听器(以获取实时更新等)。如果我在需要查询或存储时立即调用 getInstance
,或者如果我在应用程序启动时将此实例存储在静态 class 中,然后尽快使用此静态 class 属性我需要查询或存储:这两种情况在技术上是相同的。因为 Google 使用单例模式 (getInstance()
).
但是我错过了什么吗?将此实例存储为静态 class 属性并在需要时使用它真的安全吗?在我需要的时候调用 getInstance
真的安全吗? 更明确地说:在两次调用 getInstance()
之间(或两次访问静态 class 属性之间),即: 两点之间执行时间,是否存在网络连接、套接字连接、实时侦听器(快照)连接等松动的风险?
如果是:如何处理这些问题?
I would want to query and store data, using or not listeners (to get realtime updates, etc.)
不使用侦听器就无法获取数据甚至实时更新。 Cloud Firestore 中的一切都与听众有关。
if I store this instance in a static class when my app starts and then use this static class attribute as soon as I need to query or store
不要将 Android 上下文 class 放在静态字段中。对具有指向 Context 的字段上下文的 FirebaseFirestore 的静态引用将导致内存泄漏。
静态字段会泄漏上下文。非静态内部 classes 具有对其外部 class 的隐式引用。例如,如果那个外部 class 是片段或 Activity,那么这个引用意味着 long-运行 handler/loader/task 将持有对 activity 的引用防止它被垃圾收集。
因此,不要将其存储为静态变量,而是在需要时调用 getInstance()
。或者更方便的解决方案是使用依赖注入。 Dagger 可以帮你解决。
And is it really safe to call getInstance whenever I need it?
是的。
between 2 accesses to the static class attribute), i.e.: between 2 points of execution time, is there any risk to loose network connection, socket connection, realtime listeners (snapshots) connection, etc. ?
请看上面的解释。
为了补充 Alex Mamo 的回答,并避免在任何地方重复 FirebaseFirestore.getInstance()
,我声明了一个名为 db
的静态方法,其中 returns getInstance 的结果:
public static FirebaseFirestore db(){
return FirebaseFirestore.getInstance();
};
然后我就这样使用它:
db().collection(...)
Firebase Firestore 提供了 getInstance()
方法,它打开一个套接字(只有一个,在应用程序执行的任何时候)并实例化 Firestore 客户端。
我想查询和存储数据,使用或不使用侦听器(以获取实时更新等)。如果我在需要查询或存储时立即调用 getInstance
,或者如果我在应用程序启动时将此实例存储在静态 class 中,然后尽快使用此静态 class 属性我需要查询或存储:这两种情况在技术上是相同的。因为 Google 使用单例模式 (getInstance()
).
但是我错过了什么吗?将此实例存储为静态 class 属性并在需要时使用它真的安全吗?在我需要的时候调用 getInstance
真的安全吗? 更明确地说:在两次调用 getInstance()
之间(或两次访问静态 class 属性之间),即: 两点之间执行时间,是否存在网络连接、套接字连接、实时侦听器(快照)连接等松动的风险?
如果是:如何处理这些问题?
I would want to query and store data, using or not listeners (to get realtime updates, etc.)
不使用侦听器就无法获取数据甚至实时更新。 Cloud Firestore 中的一切都与听众有关。
if I store this instance in a static class when my app starts and then use this static class attribute as soon as I need to query or store
不要将 Android 上下文 class 放在静态字段中。对具有指向 Context 的字段上下文的 FirebaseFirestore 的静态引用将导致内存泄漏。
静态字段会泄漏上下文。非静态内部 classes 具有对其外部 class 的隐式引用。例如,如果那个外部 class 是片段或 Activity,那么这个引用意味着 long-运行 handler/loader/task 将持有对 activity 的引用防止它被垃圾收集。
因此,不要将其存储为静态变量,而是在需要时调用 getInstance()
。或者更方便的解决方案是使用依赖注入。 Dagger 可以帮你解决。
And is it really safe to call getInstance whenever I need it?
是的。
between 2 accesses to the static class attribute), i.e.: between 2 points of execution time, is there any risk to loose network connection, socket connection, realtime listeners (snapshots) connection, etc. ?
请看上面的解释。
为了补充 Alex Mamo 的回答,并避免在任何地方重复 FirebaseFirestore.getInstance()
,我声明了一个名为 db
的静态方法,其中 returns getInstance 的结果:
public static FirebaseFirestore db(){
return FirebaseFirestore.getInstance();
};
然后我就这样使用它:
db().collection(...)