-2

I am fetching data from Firestore but I am worried that if the data will become huge someday, is saving to state the right approach to follow?

How much data can a state actually handle?

Can I just fetch the first 100 objects and then depending on user filter fetch other requested data (is this the right thing to do)?

showtime
  • 1
  • 1
  • 17
  • 48
  • 1
    Yes, you can. It's better. – James Liu Oct 19 '19 at 02:37
  • @JamesLiu can you elaborate further in details, how do I do that, how much data can state handle, etc? – showtime Oct 19 '19 at 13:43
  • 1
    I think that how much data is determined by JS engine's capability. But the more important question is what is the user's expectation? How long to wait? In most cases, it's should be as quick as possible. I strong recommend you load a part of data and load more when the FlatList reached bottom. There are many examples to demostrate how to implement such manner. – James Liu Oct 19 '19 at 15:12
  • @JamesLiu I was trying to search for how to retrieve limited amount of data on user`s request from Firestore but was not able to find something relevant. Are you able to help me in this aspect? I only found something like: you could query for the first 3 cities alphabetically with citiesRef.orderBy("name").limit(3) . But I would like to fetch on request, so, for example, fetch 10 next ones when user requests. How can I fetch NEXT items ? – showtime Oct 20 '19 at 01:48

3 Answers3

1

Ok, I think I got what you really want.

  • do you need to implement pagination or lazy loading? It depends on how much data you are fetching. And here you are asking about a huge amount of data so yes it's better to implement pagination or lazy loading for this data in your app.Also as Firebase Firestore charges per requests, if you fetch a lot of data you will pay for their requests. However maybe your user will check only the first 5 records and you will lose a lot of money because you are paying for those requests.
  • why? There are many reasons but I guess most importantly to decrease memory intensive workload on your server as fetching huge amount of data will take probably some time to be achieved and this will effect eventually user experience also.
  • How? I suggest that you fetch a portion of your data. let's say 200 records. paginate through them and before they finish you can fetch another 200 records. This way your user will not wait for a long time and you will reduce workload on your servers. Also, don't forget you will pay less this way and give users only what they want.

Now to really know how to implement pagination or lazy loading I suggest that you check other solutions on stackoverflow as this has already been answered, you can find an example here. Also there is this official explanation by Firebase engineer on how to implement pagination and why.

Methkal Khalawi
  • 2,368
  • 1
  • 8
  • 13
  • thanks! I have managed to implement lazy loading but if I want to use snapshot() listener then it makes lazy loading meaningless. Is there a way to listen and fetch data on real-time while using lazy loading? Second, can I search for a "restaurant" while using lazy loading? The restaurant that I am searching for might not be fetched yet, so how would I be able to search for it directly in Firestore? – showtime Oct 30 '19 at 22:48
  • 1
    I have found a great solution for [pagination] (https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android). also notice that if you want to use realtime data with pagination you will need to use `onSnapshot()` listener instead of `get()` method in your code. for making queries while in real time fetching, I'm not sure if it's possible. I'll investigate more and come back you. – Methkal Khalawi Oct 31 '19 at 14:24
  • the thing is that I am using onsnapshot() but I think what this listener does is, it fetches all data after something has been added, isnt that right?. – showtime Oct 31 '19 at 23:30
  • yeah, it fetches all the data. I think you have to choose what suits you more either real time fetching or pagination(or lazy loading). you cannot do both together. For your case, I think I would choose pagination(or lazy loading). – Methkal Khalawi Nov 01 '19 at 11:53
  • that can't be the only solution, I am pretty sure there are plenty of cases like mine, how did they do it? – showtime Nov 03 '19 at 13:55
  • I just thought about something, what if I use the where() so I can only listen to the latest discounts that are added? – showtime Nov 03 '19 at 22:53
0

you should really not constrain yourself with how to get next items. As Firebase provide a powerful way to query data and get what you want. For example by using where() method and make SQL queries like to get the exact data that you want from your Firestore DB. you can check this tutorial it's super helpful. also give the documentation a look regarding Firestore queries.

Methkal Khalawi
  • 2,368
  • 1
  • 8
  • 13
  • you didnt understand the question, I basically am asking if I need to implement pagination or lazy loading and how? – showtime Oct 29 '19 at 12:12
0

I think you need to try react native pagination for maintain application flow while handling huge amount of data

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 14:09