快速查询firebase数据库的中间部分 [英] query mid-section of firebase database in swift

查看:51
本文介绍了快速查询firebase数据库的中间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将firebase用于大型数据库,每个条目都使用autoid键.要获得最后十个条目,我可以使用:

I'm using firebase for a large-ish database, with each entry using an autoid key. To get, say, the last ten entries, I can use:

ref.queryLimitedToLast(10).observeSingleEventOfType(.Value, withBlock: { snapshot in
            for item in snapshot.children {
                //do some code to each item
            }
        })

但是,我无法终生解决如何仅获得十个条目.例如.如果数据库有100个条目,我的代码将返回90-100,但是我将如何获得80-90的条目(例如,不查询最后20个并扔掉一半,因为看起来效率低下)?

However, I can't for the life of me work out how to then get just the ten entries before that. Eg. if the database had 100 entries, my code would return 90-100, but how would I then get entries 80-90 (without, for example, querying the last 20 and throwing half away, as it seems inefficient)?

我最终使用

ref.queryOrderedByChild("timecode").queryEndingAtValue(final).queryLimitedToLast(10).observeSingleEventOfType(.Value, withBlock: { snapshot in
for item in snapshot.children {
                //do some code to each item, including saving a new value of 'final'
            }
        })

,并将值"final"保存为上次更新的时间码.也就是说,首先我会得到90-100的结果,然后将90的时间码保存为最终的(减去一秒钟),然后将其用作结束值,以此类推...以找到80-89的结果.就像杰伊在下面描述的一样,但是使用时间戳而不是索引号(因为它已经在其中了)

and saving the value 'final' as the timecode of the last update. that is, first i would get results, 90-100, say, and save the timecode of 90 as final (minus one second), then use this for the ending value, etc... to find results 80-89. Just as Jay describes below, but using a timestamp instead of an index number (as it was already in there)

另外,为了使其更好地工作,我还为数据库的firebase规则添加了".indexOn":"timecode"

Edit 2: Also, to get it working better, I also added ".indexOn": "timecode" to the firebase rules for the database

推荐答案

有两种方法可以做到这一点,但是一个简单的解决方案是将total_count保留在另一个节点中,并在每个节点中保留一个索引.

There's a couple of ways to do this but an easy solution is to keep a total_count in another node and an index within each node.

然后使用queryStartingAtValue和queryEndingAtValue查询您感兴趣的子节点的范围.

Then use queryStartingAtValue and queryEndingAtValue to query the range of child nodes you are interested in.

例如,当您将一个子项添加到"posts"节点时,将一个子项添加到total_count节点并保存.随着时间的流逝,您将拥有100个帖子,total_count节点的值将为100.然后,您可以查询任意范围的帖子:.queryStartingAtValue(80)和.queryEndingAtValue(89)或.queryStartingAt(20)和.queryEndingAt(30)

When you add a child to your 'posts' node for example, add one to the total_count node and save it. Over time you'll have 100 posts and the total_count node will have a value of 100. You can then query for any range of posts: .queryStartingAtValue(80) and . queryEndingAtValue(89), or .queryStartingAt(20) and .queryEndingAt(30)

例如,假设有45个帖子(此处仅显示4个帖子)

For example, assume there's 45 posts (showing just 4 of them here)

posts
  ...
  post_1024
    text: "my post!"
    index: 42
  post_1025
    text: "another post"
    index: 43
  post_1026
    text: "yippee"
    index: 44
  post_1027
    text: "Stuff and Things"
    index: 45

然后是一个跟踪它们的节点

and then a node to track them

post_info
   total_count: 45

以及用于查询中间两个节点的代码

and the code to query for the middle two nodes

let ref = myRootRef.childByAppendingPath("posts"
ref.queryOrderedByChild("index").queryStartingAtValue(43).queryEndingAtValue(44)
   .observeEventType(.Value, withBlock: { snapshot in
    print(snapshot.key)
})

输出将是

  post_1025
    text: "another post"
    index: 43
  post_1026
    text: "yippee"
    index: 44

话虽如此,这可能会有些多余,具体取决于数据所发生的情况.如果您从不删除帖子,那么您就可以开始设置了.但是,如果删除帖子,则索引中显然会出现空白(42、43 .. 45),因此需要考虑其他因素.

That being said, this may be slightly redundant depending on what happens to your data. If you never delete posts, then you're set. However, if you delete posts then obviously there's a gap in your indexes (42, 43, .. 45) so other factors need to be taken into consideration.

您甚至可能不需要total_count-这仅取决于您的应用程序的工作方式.

You may not even need a total_count - it just depends on how your app works.

您还可以在节点上利用优先级变量来存储索引,而不是使其成为子节点.

You could also leverage the priority variable on your nodes to store the index instead of having it be a child node.

带有.Value和.numChildren的过渡和.observeSingleEvent也可以用于获取活动节点数.

Transitions and .observeSingleEvent with .Value and .numChildren can be also be used to obtain a live node count.

这篇关于快速查询firebase数据库的中间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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