问题
As I know Firebase on Flutter will automatically read from cache first but I noticed while development that an app just play around with almost 1 stream that has 10 or 15 document I have reach reads above 4000 reads! By this way if 10 users has used my app I will pay all I have, so I have recheck every query, snapshot and put this code to know
print(itemDoc.metadata.isFromCache ? "itemDoc NOT FROM NETWORK" : "itemDoc FROM NETWORK");
Edit write the code and more explain
I noticed this issue appear in IOS when I update,add,delete a document console print all stored documents.
I have Main Stream that I get all users lists then every list I create stream for it to listen list's items
userListsStream(uid){
Stream<QuerySnapshot> shoppingListsStream =
Firestore.instance.collection('lists').where('owner', arrayContains: uid).snapshots();
shoppingListsStream.listen(
(QuerySnapshot listsQuery) async {
List<DocumentSnapshot> listsDocs = listsQuery.documents;
if (listsDocs.length != 0) {
//? foreach on lists Documents
listsDocs.forEach(
(DocumentSnapshot listDoc) async {
print(listDoc.metadata.isFromCache ? "listDoc NOT FROM
NETWORK" : "listDoc FROM NETWORK");
listItemsStream(listDoc.documentID);
}
)
}
})
}
listItemsStream(lid){
shoppingItemsRef = Firestore.instance.collection('lists').document(lid).collection('items');
shoppingItemsRef.snapshots().listen(
(QuerySnapshot itemsQuery) async {
List<DocumentSnapshot> itemsDocs = itemsQuery.documents;
if (itemsDocs.length != 0) {
itemsDocs.forEach(
(DocumentSnapshot itemDoc) async {
print(itemDoc.metadata.isFromCache ? "itemDoc NOT FROM
NETWORK" : "itemDoc FROM NETWORK");
}
)
}
}
those two methods in Provider that I call the main function in Home.dart initState
@override
void initState() {
Provider.of<ListsProvider>(context, listen: false)
.userListsStream(uid);
}
回答1:
The isFromCache flag indicates whether the document is guaranteed to be up to date with the server, or that it may be a stale result from the cache. If it's false that doesn't necessarily means the document was read from the server, but that you can be guaranteed that the document is up to date with the server.
To show what this means, I can this snippet
var snapshotsStream = Firestore.instance.collection("chat").orderBy("timestamp", descending: true).limit(3).snapshots();
snapshotsStream.listen((querySnapshot) {
print("We got a new QuerySnapshot");
querySnapshot.documents.forEach((doc) {
print(doc.documentID+": "+(doc.metadata.isFromCache ? "from CACHE " : "from SERVER "));
});
querySnapshot.documentChanges.forEach((docChange) {
print(docChange.type.toString()+(docChange.document.metadata.isFromCache ? "doc from CACHE " : "doc from SERVER "));
});
}, onError: (error) {
print(error);
});
The output I get initially is:
flutter: We got a new QuerySnapshot
flutter: 5nAr5pYgwXJ0n3pWZkLw: from SERVER
flutter: moysGY7Ea7TCf28fcEVC: from SERVER
flutter: PuNnPaiLMIE7704R9NuL: from SERVER
flutter: 5nAr5pYgwXJ0n3pWZkLw: DocumentChangeType.addeddoc from SERVER
flutter: moysGY7Ea7TCf28fcEVC: DocumentChangeType.addeddoc from SERVER
flutter: PuNnPaiLMIE7704R9NuL: DocumentChangeType.addeddoc from SERVER
Then when I make a change on the server, it prints:
flutter: We got a new QuerySnapshot
flutter: 5nAr5pYgwXJ0n3pWZkLw: from SERVER
flutter: moysGY7Ea7TCf28fcEVC: from SERVER
flutter: PuNnPaiLMIE7704R9NuL: from SERVER
flutter: 5nAr5pYgwXJ0n3pWZkLw: DocumentChangeType.modifieddoc from SERVER
So using the isFromCache property is not meant to determine whether the document was a charged read on the server, but whether the document is guaranteed to be up to date with the server.
To know what documents have changed, you can iterate over the documentChanged collection, as shown in the code above.
As to having more reads than you expected, one of the more common causes of this is keeping Firestore panel open in the Firebase console. Reads performed by the console are charged towards your project. For more on this, see:
- Google Cloud Firestore console reading of all documents and charges
- Firestore collection listeners enormous number of reads
- Firestore - unexpected reads
来源:https://stackoverflow.com/questions/59627868/firebase-always-reads-from-internet