本文介绍了云Firestore:通过直接获取缓存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 从服务器检索数据可能需要几秒钟的时间。有没有办法检索缓存的数据同时,使用直接获取? onComplete 似乎是仅在从服务器检索数据时调用: db.collection(cities)。whereEqualTo(state, (@NonNull Task< DocumentSnapshot>任务){())。get() .addOnCompleteListener(new OnCompleteListener< DocumentSnapshot>(){ @Override public void onComplete if(task.isSuccessful()){ ... } } }); 是否有缓存数据的回调? 解决方案我刚刚在Android应用程序中运行了一些测试,看看它是如何工作的。 你需要的代码是同样,无论你是从缓存还是从网络获取数据: db.collection(translations) (@NonNull Task< DocumentSnapshot>任务){ DocumentSnapshot>(){ @Override 公共无效的onComplete(新的OnCompleteListener< DocumentSnapshot>快照= task.getResult(); System.out.println(isFromCache:+ snapshot.getMetadata()。isFromCache()); } }); 当我在线时,会打印: isFromCache:false 当我离线时,它会打印: isFromCache:true 无法强制从缓存,而您连接到服务器。 如果我使用一个侦听器: 文档(rPpciqsXjAzjpComjd5j)。addSnapshotListener(new DocumentListenOptions()。includeMetadataChanges(),new EventListener< DocumentSnapshot>(){ @Override )public void onEvent(DocumentSnapshot snapshot,FirebaseFirestoreException e){ System.out.println(listen.isFromCache:+ snapshot.getMetadata()。isFromCache()); } } ); 当我在线时,我得到两张照片: isFromCache:true $ b isFromCache:false Retrieving data from the server may take some seconds. Is there any way to retrieve cached data in the meantime, using a direct get?The onComplete seems to be called only when the data is retrieved from the server:db.collection("cities").whereEqualTo("state", "CA").get() .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { ... } } });Is there any callback for the cached data? 解决方案 I just ran a few tests in an Android app to see how this works.The code you need is the same, no matter if you're getting data from the cache or from the network: db.collection("translations").document("rPpciqsXjAzjpComjd5j").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { DocumentSnapshot snapshot = task.getResult(); System.out.println("isFromCache: "+snapshot.getMetadata().isFromCache()); } });When I'm online this prints: isFromCache: falseWhen I go offline, it prints: isFromCache: trueThere is no way to force retrieval from the cache while you're connected to the server.If instead I use a listener: db.collection("translations").document("rPpciqsXjAzjpComjd5j").addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() { @Override public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) { System.out.println("listen.isFromCache: "+snapshot.getMetadata().isFromCache()); } } );I get two prints when I'm online: isFromCache: true isFromCache: false 这篇关于云Firestore:通过直接获取缓存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 20:36