gpt4 book ai didi

android - 云 Firestore : retrieving cached data via direct get?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:09 24 4
gpt4 key购买 nike

从服务器检索数据可能需要几秒钟。有没有办法同时使用直接获取来检索缓存数据?

onComplete 似乎仅在从服务器检索数据时被调用:

db.collection("cities").whereEqualTo("state", "CA").get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
...
}
}
});

缓存数据是否有回调?

最佳答案

现在可以只从缓存版本加载数据。来自 docs

You can specify the source option in a get() call to change the default behavior.....you can fetch from only the offline cache.

如果失败,您可以重新尝试在线版本。

例子:

DocumentReference docRef = db.collection("cities").document("SF");

// Source can be CACHE, SERVER, or DEFAULT.
Source source = Source.CACHE;

// Get the document, forcing the SDK to use the offline cache
docRef.get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
// Document found in the offline cache
DocumentSnapshot document = task.getResult();
Log.d(TAG, "Cached document data: " + document.getData());
} else {
Log.d(TAG, "Cached get failed: ", task.getException());
//try again with online version
}
}
});

关于android - 云 Firestore : retrieving cached data via direct get?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47222713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com