Firebase&快速使用.index对数据快照进行排序

编程入门 行业动态 更新时间:2024-10-26 06:37:16
本文介绍了Firebase&快速使用.index对数据快照进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是Firebase / swift新手,我正尝试使用.indexOn对服务器上的一组节点进行排序。我已经搜索了SO和文档来获取规则编写,但我正在努力与swift代码使用索引正确的格式。

我的假设是当我使用.indexOn规则时,返回的快照使用该索引来排序结果。通过在swift中使用.queryOrderedByChild(title),我否定了创建索引的服务器端性能优势。

以下是我的Firebase数据库结构的简化片段: <$ p $用户名:{GkdjgdBJh1TxuAzIPezLWRouG9f2:{messages:{-KM0crettRl-RLQQdmMQ:{body:任何人都需要一点体字符串?,title:5另一个标题字符串,-KM0dhkChY6ZQ2QzuPlC:{body:This is a short body string,title:1可爱的标题字符串,},-FQ0dhkChY6ZQ2Qzu3RQv:{body:短而甜美的身体字符串,title:3我应该在中间,} } } } }

这是我的规则JSON:

{rules:{.read:true,.write:true, 用户:{$ userid:{ 消息:{.indexOn:[title] } } } } }

这是我的swift代码:

if user = FIRAuth.auth()?currentUser { //用户登录 // ***我认为这是问题的路线*** FIRDatabase.database()。reference()。child(users)。child(user.uid).child(messages)。$ b $中的observeEventType(.Value,withBlock:{ b messageArray = [] 如果让dictionaryOfMessages = snapshot.value as? [String:AnyObject] { for dictionaryOfMessages.keys中的messageKey { messageArray.append(Message(json:JSON(dictionaryOfMessages [messageKey]!))) //设置messageId messageArray [messageArray.count - 1] .messageId = messageKey } } //将数据返回给函数 success(messages:messageArray) }){(error)in //处理错误} } else { //返回一些关于登录等的通用消息)

任何建议如何修改我的Swift代码来使用索引(或更正索引if这是错误的)将是伟大的。

解决方案

您需要使用 queryOrderedByChild 数据,并像你一样在你的规则中有一个关键。这里是一个例子;

Firebase文档

下面的例子演示了如何检索用户的顶部帖子列表,按星星数排序:

//我的热门文章数量为 let myTopPostsQuery =(ref.child(user-posts)。child(getUid()))。queryOrderedByChild(starCount)

该查询根据用户ID从数据库中的路径中检索用户的帖子,按照每个帖子收到的星号排序。这种使用ID作为索引键的技术称为数据扇出(data fan out)。

对queryOrderedByChild方法的调用指定子键以排序结果。在这种情况下,帖子按每个帖子中的starCount子元素的值排序。有关如何订购其他数据类型的更多信息,请参阅如何查询数据。

请点击链接了解更多详情;

firebase.google/docs/database / ios / retrieve-data

I'm a Firebase/swift newbie and I'm trying to use .indexOn to sort a set of nodes on the server. I've searched SO and the docs to get the rules written but I'm struggling with the correct format for the swift code to use the index.

My assumption is that when I use an .indexOn rule the returned snapshot uses that index to order the result. By using .queryOrderedByChild("title") in swift I negate the server-side performance benefits of creating the index. Please correct me if this assumption is incorrect.

Here's a simplified snippet of my Firebase DB structure:

{ "users": { "GkdjgdBJh1TxuAzIPezLWRouG9f2": { "messages": { "-KM0crettRl-RLQQdmMQ": { "body": "Anyone want a bit of body string?", "title": "5 Another title string", }, "-KM0dhkChY6ZQ2QzuPlC": { "body": "This is a short body string", "title": "1 A lovely title string", }, "-FQ0dhkChY6ZQ2Qzu3RQv": { "body": "Short and sweet body string", "title": "3 I should be in the middle", } } } } }

Here's my rule JSON:

{ "rules": { ".read": true, ".write": true, "users": { "$userid": { "messages": { ".indexOn": ["title"] } } } } }

Here's my swift code:

if let user = FIRAuth.auth()?.currentUser { // user is logged in // *** I think this is the line with the issue *** FIRDatabase.database().reference().child("users").child(user.uid).child("messages").observeEventType(.Value, withBlock: { (snapshot) in messageArray = [] if let dictionaryOfMessages = snapshot.value as? [String: AnyObject] { for messageKey in dictionaryOfMessages.keys { messageArray.append(Message(json: JSON(dictionaryOfMessages[messageKey]!))) // set the messageId messageArray[messageArray.count - 1].messageId = messageKey } } // return the data to the VC that called the function success(messages: messageArray) }) { (error) in // Handle the Error } } else { // return some generic messages about logging in etc. }

Any suggestions how I can modify my Swift code to use the index (or correct the index if that's wrong) would be great.

解决方案

You need to use queryOrderedByChild when you're requesting the data and have a key in your rules as you did. here's an example;

Firebase documentations

The following example demonstrates how you could retrieve a list of a user's top posts sorted by their star count:

// My top posts by number of stars let myTopPostsQuery = (ref.child("user-posts").child(getUid())).queryOrderedByChild("starCount")

This query retrieves the user's posts from the path in the database based on their user ID, ordered by the number of stars each post has received. This technique of using IDs as index keys is called data fan out.

The call to the queryOrderedByChild method specifies the child key to order the results by. In this case, posts are sorted by the value of the "starCount" child in each post. For more information on how other data types are ordered, see How query data is ordered.

Please follow the link for more details;

firebase.google/docs/database/ios/retrieve-data

更多推荐

Firebase&amp;快速使用.index对数据快照进行排序

本文发布于:2023-11-28 22:39:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快照   快速   数据   Firebase   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!