可能以相反的顺序遍历Firebase节点的子节点? [英] Possible to traverse the children of a Firebase node in reverse order?

查看:128
本文介绍了可能以相反的顺序遍历Firebase节点的子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑实现一个如下所述的in-firebase索引: https://stackoverflow.com/a/10559689/ 534086 ,并试图找出如何处理降序排序数据。如果你有一个节点与孩子A,B,C检索他们C,B,A是否可能? (虽然仍然能够以原始顺序检索它们)

如果无法向前和向后遍历节点,我需要创建两个索引,他们是一个简单的算法来生成按逆向词法顺序排序的键?对于数字,我认为你可以乘以-1,但不知道如何处理字符串...

解决方案

听起来像你想要的是一个类似于limit()现在起作用的reverse()查询。 Firebase无法做到这一点,但我们正在计划添加它。



根据您的使用情况,您可能很简单。例如,如果您正在使用Firebase的排序功能按顺序显示给用户,则可以简单地修改显示逻辑以向后显示事物(前置项而不是附加内容)。如果您要查询从一大群孩子中选择一个数据窗口(比如说,要从长列表中获得前10个项目),那么排序顺序应该不重要 - 您的开始和结束点查询将保持不变,除非他们将被颠倒。


I'm looking into implementing an in-firebase index as described here: https://stackoverflow.com/a/10559689/534086, and trying to figure out how to deal with sorting data in descending order. Is it possible if you have a node with children 'a', 'b', 'c' to retrieve them 'c', 'b', 'a'? (while still being able to retrieve them in the original order as well?)

If it's not possible to traverse the node both forwards and backwards and I need to create two indexes, is their an easy algorithm for generating keys that sort in reverse lexographic order? For numbers, I think you can multiply by -1, but not sure how to handle strings...

解决方案

It sounds like what you want is a "reverse()" query similar to how "limit()" works now. Firebase can't do this yet, but we're planning to add it.

Depending on your use case, it might be pretty simple for you to get around this though. If, for instance, you're using Firebase's sorting to keep a list in order that is being displayed to the user, you could simply modify the display logic to render things backward (prepend items instead of append for instance). If you're querying to select a window of data out of a large set of children (say, to get the first 10 items out of a long list), the sort order shouldn't be important -- your start and end points for the query will remain the same except they'll be reversed.

这篇关于可能以相反的顺序遍历Firebase节点的子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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