0 android offline-caching kotlin firebase google-cloud-firestore
所以我的一半应用程序在很大程度上依赖于 Firestore。
5000ms有时,加载我的文档需要相当长的时间,甚至更长的时间。如果它是图像或其他东西,也许我会理解,但它主要是字符串或整数......
关于如何改进这个的任何想法?
谢谢
编辑: db.collection("usersAuth/${FirebaseAuth.getInstance().uid!!}/KitLists").get().addOnSuccessListener { 快照 ->
for (document in snapshot.documents) {
val data = document
val kitName = data.id
firstKitList.add(kitName)
}
mainListViewAdapter.notifyDataSetChanged()
}
Run Code Online (Sandbox Code Playgroud)
编辑2
因此,我对其进行了调整,但我在snapshot.
db.collection("usersAuth/${FirebaseAuth.getInstance().uid!!}/KitLists").addSnapshotListener(object : EventListener<QuerySnapshot> {
override fun onEvent(@Nullable value: QuerySnapshot, @Nullable e: FirebaseFirestoreException?) {
if (e != null) {
Log.w("TAG", "Listen failed.", e)
return
}
for (document in snapshot.documents) {
val data = document
val kitName = data.id
firstKitList.add(kitName)
}
mainListViewAdapter.notifyDataSetChanged()
}
})
Run Code Online (Sandbox Code Playgroud)
这是错误
如果您使用get()电话,您需要知道您正在尝试通过互联网读取数据。您无法将此操作与读取本地存储在磁盘上的 SQLite 数据库的操作进行比较。从 Firebase 服务器获取数据的速度取决于您的互联网连接速度以及您尝试获取的数据量。因此,等待的原因很可能5000ms是其中之一,或者为什么不是两者兼而有之。如果原因是数据量,请尝试优化查询或尝试获取小部分数据。
如果我们谈论的是第一次尝试读取文档,它可能会比后续的慢,因为它必须启动互联网连接。我知道 Firebase 团队正在努力提高性能,但0ms在通过网络检索数据时你不能指望。
您可以做的一件事是启用offline persistence它将为之前读取的数据创建本地缓存。但get()请求会首先检查 Firebase 服务器。如果您使用addSnapshotListener(),您将能够立即从缓存中读取数据,而无需检查网络。
如果您想听单个文档,请使用以下代码:
yourDocumentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
if (snapshot != null && snapshot.exists()) {
Log.d(TAG, "Current data: " + snapshot.getData());
} else {
Log.d(TAG, "Current data: null");
}
}
});
Run Code Online (Sandbox Code Playgroud)
如果您想收听集合中的多个文档,请使用以下代码:
yourCollectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
List<String> cities = new ArrayList<>();
for (DocumentSnapshot doc : value) {
if (doc.get("name") != null) {
cities.add(doc.getString("name"));
}
}
Log.d(TAG, "Current cites in CA: " + cities);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1756 次 |
| 最近记录: |