有效地查询 firebase [英] querying firebase efficiently

查看:32
本文介绍了有效地查询 firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从每个用户名值中搜索我的用户群.从我在网上看到的情况来看,人们经常返回所有 用户,然后在表格视图中过滤它们,但这似乎不切实际也不可行.我的想法是查询数据并返回一个指数级较小的值数组,但是我在使用提供的查询方法时遇到了问题.

I want to search my user base from each users name value. From what I've seen online people often return all users then filter them in a table view but that doesn't seem practical nor viable. My thought was to query data and return an exponentially smaller array of values but I am having trouble using the query methods provided.

如何查询数据库的特定方面?

我如何构建我的代码以使其可行;不是每个用户都加载,一次最多加载 10 个.

How do I structure my code so that it's viable; not loading in EVERY user, something like 10 max at a time.

非常感谢任何建议、资源和链接.

Any suggestions, resources, and links are greatly appreciated.

我做了一些研究,看起来 Firebase 带有一些内置的查询方法......到目前为止,这是我试图用下面的代码测试它以打印出以 I 开头的用户,但我可以不要让它在控制台中打印任何用户

I did some researching and it looks like Firebase comes with some built in querying methods... So far this is what I'm attempting to test it out with the code below to print out users starting with I, but I can't get it to print any users in the console

 ref.queryOrderedByKey().queryStarting(atValue: "I").queryEnding(atValue: "Iu{f8ff}")
        .observe(.childAdded, with: { snapshot in
            print(snapshot.key)
        })

推荐答案

有多种解决方案,但往往加载所有用户数据时数据太多.

There are a number of solutions and often times loading ALL of the user data is too much data.

这是一个典型的用户节点

Here's a typical users node

users
  uid_0
    name: "Jean Luc"
  uid_1
    name: "Will"
  uid_2
    name: "Geordi"

一种选择是遍历每个用户节点,一次一个,以检索用户名.这完全避免了庞大的数据集.我们将使用 .childAdded 事件来加载每个并存储在一个数组中

One option is to iterate through each user node, one at a time, to retrieve the user name. This avoids an enormous data set entirely. We'll use the .childAdded event to load each and store in an array

    let usersRef = self.ref.child("users")
    var userNamesArray = [String]()

    usersRef.observe(.childAdded, with: { snapshot in
        let userDict = snapshot.value as! [String: Any]
        let name = userDict["name"] as! String
        userNamesArray.append(name)
    })

第二种选择是将用户名存储在一个完全不同的节点中,这显着减少了混乱",因为其余数据保留在主用户节点中

A second option is to store the user name in an entirely different node, which significantly reduces the 'clutter' as the rest of the data remains in the main users node

user_names
   uid_0: "Jean Luc"
   uid_1: "Will"
   uid_2: "Geordi"

正如您在此结构中所见,即使有数千个名称,它也只是占用空间很小的文本.

As you can see with this structure, even with thousands of names it's just text with a very small footprint.

另一种选择是使用 .startingAt 和 .endingAt 一次加载 X 个用户,并遍历返回的用户以获取每个名称.在这种情况下,我们希望所有用户都以 A 开头并以 M 结尾......抱歉,Worf.

Another option is to load X number of users at a time using .startingAt and .endingAt and iterate over the returned users to get each name. In this case we want all users starting with A and ending with M... Sorry Worf.

    let usersRef = self.ref.child("users")
    var userNamesArray = [String]()

    let nameQuery = usersRef.queryOrdered(byChild: "name")
                            .queryStarting(atValue: "A")
                            .queryEnding(atValue: "Mu{f8ff}")
    nameQuery.observe(.value, with: { snapshot in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let userDict = snap.value as! [String: Any]
            let name = userDict["name"] as! String
            userNamesArray.append(name)
        }
    })

最后一个例子以A开头的用户名开始,以M结尾的用户名+一个非常高的unicode字符结束,这使得它包含了所有以M开头的名字

The last example started with users names starting with A and ended with user names ending with M + a very high unicode character, which makes it inclusive for all names starting with M

上面查询中使用的 uf8ff 字符是一个非常高的代码点在 Unicode 范围内.因为它在大多数常规字符之后Unicode,查询匹配所有以 queryString 开头的值.

The uf8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with queryString.

这篇关于有效地查询 firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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