因此,有两种情况没有网络:
没有使用任何查询,我得到一个错误:
collectionReference.document().get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: success");
} else {
Log.d(TAG, "onComplete: " + task.getException());
}
}
});
addOnFailureListener
也是一样,在这里我可以记录上述示例的错误,即:
Failed to get document because the client is offline
现在,在使用查询时,我没有出现错误,示例:
collectionReference.limit(1).get().addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.getMessage());
}
}).addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
Log.d(TAG, "onSuccess: ");
}
});
使用了limit
选项。其他案例:
collectionReference.addSnapshotListener((queryDocumentSnapshots, e) -> {
if (e != null) {
Log.d(TAG, "onEvent: error" + e);
return;
}
if (queryDocumentSnapshots != null) {
Log.d(TAG, "onEvent: success");
} else {
Log.d(TAG, "onEvent: no result");
}
});
或使用document
:
collectionReference.document("path").addSnapshotListener((documentSnapshot, e) -> {
if (e != null) {
Log.d(TAG, "onEvent: error" + e);
return;
}
if (documentSnapshot != null) {
Log.d(TAG, "onEvent: success");
} else {
Log.d(TAG, "onEvent: no result");
}
});
另一个返回success
的人:
collectionReference.limit(1).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: success");
} else {
Log.d(TAG, "onComplete: error " + task.getException());
}
}
});
当前依赖项:
implementation 'com.google.firebase:firebase-firestore:17.1.0'
以上所有示例即使失败也会返回success
。那么,这是错误还是应该像这样工作?