如何抓取多个路径并对它们进行排序并将结果带回Firebase? [英] How to grab multiple paths and sort them and bring back the result in firebase?
问题描述
但是,我的数据看起来像这样:
如下所示:
:{
UserId:{
projId1:true,
projId2:true
}
}
项目:{
projId1 :{
title:SomeText,
date:TimeStamp
}
}
< Firebase查询运行在一个特定的位置,它只能对位于该位置子项下的固定路径上的数据进行排序。对于存在于不同路径的数据,无法对Firebase查询进行排序。
因此,对于您的情况,如果要从 / followed / $ uid
,没有办法从 / projects
节点的数据中排序结果。
给你两个选择:
$ b
-
对客户端上的数据进行排序
-
在
的排序属性后面
子
只要您的数据集不是太大,并且不会过滤太多,就可以在客户端上对数据进行排序。
$ b $但是要允许在服务器上进行排序,您必须在您查询的节点下复制相关属性:
UserId:{
projId1:TimeStamp,
projId2:TimeStamp
}
}
项目:{
projId1:{
title:SomeText,
date:TimeStamp
}
}
使用此结构,您可以使用
查询查询= database.child(user.getUid())。orderByValue();
query.addChildEventListener(...
Hey guys so I am trying to understand how I can take a list of Ids, and then ask for Firebase to take each path and sort them by date and bring me back the top 20 all in one Firebase call. At the moment I am doing this by looping through the ids and grabbing each path - adding the items to a list then sorting them.
However this is inefficient especially as the list gets bigger.
My data looks something like this:
follows: {
UserId :{
projId1: true,
projId2: true
}
}
projects: {
projId1 :{
title: SomeText,
date: TimeStamp
}
}
A Firebase query runs at one specific location and it can only order on data that exists at fixed paths under the children of that location. There is no way to sort a Firebase query on data that exists at a different path.
So in your case, if you are loading data from /follows/$uid
, there is not way to order the results on data from the /projects
nodes.
That leaves you with two options:
sort the data on the client
at the sort properties to the
follows
children
Sorting data on the client is a valid option as long as your data set is not too large and you're not filtering too much.
But to allow sorting on the server, you'll have to duplicate the relevant properties under the node where you query:
follows: {
UserId :{
projId1: TimeStamp,
projId2: TimeStamp
}
}
projects: {
projId1 :{
title: SomeText,
date: TimeStamp
}
}
With this structure you can order then projects on timestamp with
Query query = database.child(user.getUid()).orderByValue();
query.addChildEventListener(...
这篇关于如何抓取多个路径并对它们进行排序并将结果带回Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!