我正在使用Firebase Firestore,我遇到了读取操作的问题: 我使用onCompleteListener,在那里,如果操作成功或不成功,我会调用不同的回调。 问题是,如果存在网络问题,则调用onCompleteListener,但task.isSuccessfull返回true!所以我得到一个空的结果,我无法区分真实的空结果。有没有办法区分网络问题和空读?
非常感谢!我的功能如下:
function myMain(){
// get the number of repetitions from the user and cast as a number
var num = Number(prompt('Please enter the number of times to repeat'));
// loop from 0 to the number the user provided - 1 hence the <
for (var i = 0; i < num; i++){
// alert the iteration of the loop, you will notice i add 1 to num when we alert it; this is because it starts at 0 so the first time it displays it would show 'Stats Solver execution 0 ======' instead of 'Stats Solver execution 1 ======'
alert('Stats Solver execution ' + (num + 1) + ' ===============');
// call your wall function
wall();
// go back to the top of the loop
}
// alert that we are done.
alert('done===============')
}
// your wall function goes here
// call your myMain function to kick everything off
myMain();
答案 0 :(得分:4)
如果您处于离线状态,客户端将首先尝试连接。一旦发现它无法连接,它将尝试根据本地数据库中的数据完成get()
。这是对Firestore的有效操作,因此这就是成功完成任务的原因。
您可以通过checking the metadata:querySnapshot.getMetadata().isFromCache()
检测结果是来自本地缓存与直接来自服务器的结果。来自文档:
true
如果快照是从缓存数据创建的,而不是保证最新的服务器数据。如果您的侦听器选择了元数据更新(通过MetadataChanges.INCLUDE
),则一旦客户端从后端收到最新数据,您将收到另一个isFomCache()
等于false的快照。