Firebase模拟WHERE IN [英] Firebase mimicing WHERE IN
问题描述
鉴于下面提供的日期模型,在用户登录并检索其数据之后,他们还可以获得其朋友的列表.
Given the date model provided below, after a user logs in and retrieves their data, they can also get a list of their friends.
但是,为了通过ID现在获得这两个朋友的详细信息 fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de
和 a96da7b1-7c4e-44bc-b82e-fc75bed52bcd
,目前,最有效的操作似乎只是通过以下方式循环访问网址:
However, in order to now attain details of those two friends by ids fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de
and a96da7b1-7c4e-44bc-b82e-fc75bed52bcd
,
at present, the most efficiently operation appears to simply be to loop through the urls, in such a way as:
for singleFriendID in allFriendIds
firebase.com/[my_db_name]/users/{singleFriendID}
end
但这仍然有其主要局限性,例如,如果您要列出40个朋友的用户名列表,则现在需要对服务器进行40个单独的特定呼叫...
but even that has its major limitations, if say you want a list of usernames of your 40 friends, you now need 40 individual, specific calls, to the server...
如何有效地在Firebase中查找数据,而不是将所有朋友详细信息转储到他们的手机中?
How can I efficiently lookup data within Firebase instead of dumping all the friend details to their phone?
Firebase模式:
Firebase schema:
{
"users":{
"99e4989b-a046-4c5f-9478-5ebd8bdc3ded":{
"email":"person@gmail.com",
"friends":{
"fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de":{
"confirmed":true,
"selfSendRequest":false,
"timeInvited":"2016-02-21 08:49:31"
},
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd":{
"confirmed":true,
"seldSendRequest":false,
"timeInvited":"2016-02-21 08:49:31"
}
},
"password":"aPassword",
"phoneNumber":"16144444444",
"username":"person2"
}
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd": {
...
...
}
}
推荐答案
Loolooii的方法将节省调用次数,并且绝对是有效的方法.
Loolooii's approach will save on the number of calls and is definitely a valid approach.
但是您还假设对Firebase数据库的40次调用很多.如果您花一点时间对此进行衡量,您可能会感到惊讶.
But you're also making the assumption that 40 calls to the Firebase database is a lot. If you take a moment to measure this, you'll probably be surprised.
在传统数据库中,您需要为每个调用往返数据库.
In traditional databases, you do a roundtrip to the database for each call.
client server
--- give me item 1 --->
loading
item 1
<-- here is item 1 ----
--- give me item 2 --->
loading
item 2
<-- here is item 2 ----
--- give me item 3 --->
loading
item 3
<-- here is item 3 ----
--- give me item 4 --->
loading
item 4
<-- here is item 4 ----
Firebase用管道传输所有请求.因此,基本上,客户立即连续快速发送对所有项目的请求:
Firebase pipelines all the requests. So essentially the clients immediately send the requests for all items in quick succession:
client server
--- give me item 1 --->
--- give me item 2 --->
--- give me item 3 --->
--- give me item 4 --->
loading
item 1
loading
item 2
loading
item 3
loading
item 4
<-- here is item 1 ----
<-- here is item 2 ----
<-- here is item 3 ----
<-- here is item 4 ----
您会发现这要快得多,因为您只需要等待1次往返,再加上加载项目的时间.如果涉及到最后一点,请采用Loolooii的方法.
You'll find that this is much faster, since you're only waiting for 1 roundtrip, plus the time to load the items. If that last bit is concerning, take Loolooii's approach.
这篇关于Firebase模拟WHERE IN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!