如何抓取多个路径并对它们进行排序并将结果带回Firebase? [英] How to grab multiple paths and sort them and bring back the result in firebase?

查看:141
本文介绍了如何抓取多个路径并对它们进行排序并将结果带回Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,所以我想了解如何获取ID列表,然后要求Firebase采取每条路径并按日期对其进行排序,然后在一次Firebase调用中将前20名全部退回。目前,我正在通过循环浏览ID并抓取每个路径来完成这项工作 - 将项目添加到列表中,然后对它们进行排序。



但是,我的数据看起来像这样:

 如下所示: 

:{
UserId:{
projId1:true,
projId2:true
}
}

项目:{
projId1 :{
title:SomeText,
date:TimeStamp
}
}


< Firebase查询运行在一个特定的位置,它只能对位于该位置子项下的固定路径上的数据进行排序。对于存在于不同路径的数据,无法对Firebase查询进行排序。



因此,对于您的情况,如果要从 / followed / $ uid ,没有办法从 / projects 节点的数据中排序结果。



给你两个选择:
$ b


  1. 对客户端上的数据进行排序


  2. 的排序属性后面


只要您的数据集不是太大,并且不会过滤太多,就可以在客户端上对数据进行排序。
$ 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:

  1. sort the data on the client

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆