Reputation: 3276
I'm really new to Firebase world and I'm getting trouble trying to do some stuff.
My Firebase is similar to this:
- users
+ 1
+ 2
+ 3
+ 4
...
+ n
,where the numbers are facebook users id.
I want to check which friends from some user is registered on the system (ie. on Firebase).
To do that, I want to send an JSON with all ids I want to check and receive the ids checked.
Is that possible? Or I need to check one by one?
Thanks!
Upvotes: 0
Views: 667
Reputation: 13266
Firebase currently has no way to say "give me only a subset of children of a list by id", which is equivalent to the SQL statement SELECT * FROM users WHERE id IN (...)
. However, there are two ways to handle this currently:
There are two primary ways to handle this use case:
As you mention, whenever a client is interested in retrieving this data, the client can load each of the users one by one. Creating Firebase references, by calling var ref = new Firebase(...)
or ref.child(...)
is very inexpensive, though you'll have to wait for all of the children to load to get a complete picture of the friends list. Also, this will cause you to add listeners to a number of children that don't exist, and simply have a null
value in your list.
Alternatively, consider denormalizing your data. Anant @ Firebase wrote a great blog post awhile back about this topic that I would recommend as a place to get started - Denormalizing Your Data is Normal. For this specific use case, whenever you load a list for the first time, or "reload" a user's friend list, you could create your own indexes of friends.
The difference between these two approaches is essentially who does the fan-out vs aggregation. Specifically, the second approach listed above involves writing to many locations but only one read by the consuming client, while the first approach involves writing to one location but reading from many locations by the consuming client.
Upvotes: 1