问题描述
开始探索Firestore存储和要做的第一件事-通过文档密钥(已通过Google身份验证,在我的Android应用中阅读一个简单的小文档),但这可能并不重要.这是一个代码段:
Just starting to explore Firestore storage and first thing to do - read a simple small document in my Android app by document key (authenticated with Google, but probably that's not important). Here is a snippet:
public void readDoc(final String key) {
final long start = System.currentTimeMillis();
docsCollection.document(key).get().addOnCompleteListener(
new OnCompleteListener<DocumentSnapshot>() {
@Override public void onComplete(@NonNull Task<DocumentSnapshot> task) {
long end = System.currentTimeMillis();
Log.d("FirestoreStorage", "get() time: " + (end - start));
}
});
}
这是我在LogCat中看到的内容:
Here is what I see in LogCat:
10-10 22:30:06.026 D/FirestoreStorage: get() time: 1666
10-10 22:30:08.199 D/FirestoreStorage: get() time: 264
第一次读取总是很慢,随后的读取大约为200ms.该文档非常小,当前只有4个属性,并且只有一个(int)为非null,因此大小不是问题.在真实手机上运行应用程序,在Android 7.1上运行Nexus 6
The first read is always very slow, subsequent reads are about 200ms. The document is really small, currently it's just 4 properties and only one (int) is non-null, so the size is not an issue. Running the app on a real phone, Nexus 6 on Android 7.1
问题:我在做什么错?我基本上是使用获取数据"中的示例操作指南中的内容.
Question: what am I doing wrong? I'm basically using a sample from "Getting data" of the How-to Guide.
这样的读取应该花费 0毫秒.如果没有解决方法,我想我必须放弃实时存储作为该应用程序的唯一存储的想法,然后回到普通的SQLite并将Firebase/Firestore用作独立的云存储.
A read like this should take 0 milliseconds. If there's no workaround, I guess I have to give up the idea of Realtime storage as the only storage for the app and get back to plain SQLite and use Firebase/Firestore as a separate cloud storage.
更新从版本16.0.0开始 DocumentReference.get()和 Query.get()具有一个新参数"source",该参数可控制从何处读取数据-仅服务器,仅缓存或尝试服务器然后缓存.
UPDATE Starting from version 16.0.0 DocumentReference.get() and Query.get() have a new parameter "source" that allows to control where the data is read from - only server, only cache or try server then cache.
PS Firestore存储初始化和相应的日志,抱歉,不是500ms,而是350,它是不同的,有时是400,有时是300:
PS Firestore storage initialization and corresponding logs, sorry not 500ms but 350, it's different, sometimes 400, sometimes 300:
public FirestoreStorage(String userRef) {
Log.i(TAG, "User ref: \"" + userRef + "\"");
db = FirebaseFirestore.getInstance();
Log.i(TAG, "Is persistence enabled: " + db.getFirestoreSettings().isPersistenceEnabled());
DocumentReference userDoc = db.collection("users").document(userRef);
prefsCollection = userDoc.collection("prefs");
prefsCollection.addSnapshotListener(
Executors.newFixedThreadPool(2),
new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
Log.d(TAG, "Prefs.onEvent");
}
});
Log.i(TAG, "Snapshot listener added");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
日志:
10-11 23:11:42.382 I/FirestoreStorage: User ref: "<cut>"
10-11 23:11:42.474 I/FirestoreStorage: Is persistence enabled: true
10-11 23:11:42.496 I/FirestoreStorage: Snapshot listener added
10-11 23:11:42.855 D/FirestoreStorage: Prefs.onEvent
推荐答案
这些get()
请求正在通过网络从Cloud Firestore后端读取数据,因此它们的速度肯定比仅读取数据的SQLite慢得多.从磁盘本地.第一次读取也可能会比后续读取的速度慢,因为它必须启动到后端的网络通道.我们将着眼于随着时间的推移提高性能,但是如果您要通过网络检索数据,则不能指望0毫秒.
These get()
requests are reading the data from the Cloud Firestore backend, over the network, so they'll necessarily be much slower than SQLite which is just reading locally from disk. The first read is also likely to be slower than subsequent ones since it has to initiate the network channel to the backend. We'll look at improving performance over time, but you can't expect 0 ms if you're retrieving data over the network.
您可能想要启用离线持久性您先前读取的数据的本地缓存.请注意,尽管get()
呼叫仍将尝试首先连接网络,以便为您提供尽可能最新的数据.如果您改用addSnapshotListener()
,我们将立即与您缓存的数据联系,而无需等待网络.
You may want to enable offline persistence which would enable local caching of data you've previously read. Note though that get()
calls will still try to hit the network first to give you as up-to-date data as possible. If you use addSnapshotListener()
instead, we'll call you immediately with the cached data, without waiting for the network.
这篇关于Firestore文档的get()性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!