2

I am building a hospital management app. In this app there are bunch of features of which major features are messaging, appointment booking, video appointment and much more. In the messaging feature what I am trying to do is loading all the previous messages from cache using bellow code

QuerySnapshot<Map<String, dynamic>> localMessageDocs = await _db
    .collection("users")
    .doc(AuthService.uid())
    .collection("messages")
    .orderBy('time', descending: true)
    .get(GetOptions(source: Source.cache));

and after that I am querying latest messages using below code

 _db
    .collection('users')
    .doc(AuthService.uid())
    .collection('messages')
    .where('time', isGreaterThan: _lastLocalMessageTime)
    .orderBy('time', descending: true)
    .snapshots()

but still I am afraid of bunch of reads that may occur if this system fails, like the cache size allowed in android and the firebase cache limitations(Default is 40MB) as messages for single user can be increase exponentially, which may cause thousand dollar bill. Any help will be appreciated!

jps
  • 20,041
  • 15
  • 75
  • 79
  • Why not check if each stream is cached first? in your Listview.builder: snapshot.data.docs[i].metadata.isFromCache. – krumpli Apr 13 '22 at 07:45
  • If i start up my app, scroll through my listview once it's not cached, the second time it is however https://firebase.google.com/docs/firestore/manage-data/enable-offline – krumpli Apr 13 '22 at 07:51
  • Maybe use ```.limit(10)``` and load 10 messages every time when user clicks more button. – test Apr 13 '22 at 08:02
  • actually I am only requesting the non cached documents from firestore but still I am afraid. – RUSHIKESH NARWADE Apr 13 '22 at 12:47
  • @RUSHIKESHNARWADE I am not sure if I understand you correctly. Are you loading the documents from cache and quering it and since there is a limitation, you know there wont be more than 40MB of messages in Cache but you are afraid if the data is not taken from Cache, then you will be charged for the reads. Is it correct? Then why did you mention in your previous comment you are only requesting "non-cached documents"? Can you clarify and help me understand better so that I can help solve the issue. – Priyashree Bhadra Apr 14 '22 at 12:37
  • @PriyashreeBhadra I am afraid because messages can be in thousands and considering it for thousand people this leads to millions of reads per day so this can be problem for me if cache doesn't work. – RUSHIKESH NARWADE Apr 15 '22 at 14:28
  • To check whether you're receiving data from the server or the cache, use the fromCache property on the SnapshotMetadata in your snapshot event. If fromCache is true, the data came from the cache and might be stale or incomplete. If fromCache is false, the data is complete and current with the latest updates on the server. Also you might want to have a look at the [stackoverflow thread](https://stackoverflow.com/a/47179668/15803365) which explains your use case and billing scenario. – Priyashree Bhadra Apr 19 '22 at 08:27
  • Also go through this [case summary](https://pamartinezandres.com/lessons-learnt-the-hard-way-using-firebase-realtime-database-c609b52b9afb) which pretty much sums it up, how we can avoid a thousand dollar firebase bill. Give it a read. – Priyashree Bhadra Apr 19 '22 at 08:43

0 Answers0