0

I have an issue. I'm using firebase to build an application, I have a list of Car object store in firebase and another object User which also have userCars property in which I store only Car's id

cars: [
  1: {},
  2: {},
  ...
],
users: [
  1: {
    userCars: [1, 2]
  }
]

Sometimes, I want to fetch a list of cars of a user.

  1. Using Firebase Realtime Database: I have to fetch a list of car_id then for each car_id, I have to call once to fetch the car's data, the combine together to return (=> If I have 10 cars, it look likes I call 10 request)

  2. Using Firebase Cloud Function: I will write an API & do exactly the same thing, the only difference is: I suppose when running on Cloud, once will run faster because that is on internal network.

Which option should I use? And do you know any document about how realtime database work when we make many requests at the same time.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Tran Quan
  • 1,056
  • 15
  • 28
  • 2
    "Which option should I use?" Both approaches can work. I personally prefer #1, because it maintains the realtime nature of the database. There are no performance problems, since [Firebase is able to pipeline the requests](http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786). – Frank van Puffelen Feb 08 '18 at 04:15
  • Thanks for your answer, I'm checking the documents on your comment. But if I use pipeline: send all request at the same time. I suppose when I get responses, I have to check whether all requests has responded or not to insert into the results array. That will make the logic looks a little bit weird. I think it more suitable in case I want to fetch 10 requests independently – Tran Quan Feb 08 '18 at 16:17
  • You may have to wait for all responses when doing a client side join. If that is the case, [search for `Promise.all()`](https://stackoverflow.com/search?tab=votes&q=%5bfirebase-database%5d%20Promise.all) to find examples of how to do that. – Frank van Puffelen Feb 08 '18 at 17:39

0 Answers0