有效查询Firebase [英] querying firebase efficiently
问题描述
我想从每个用户名称值中搜索我的用户群。根据我在网上看到的,人们经常返回所有的用户,然后在表格视图中过滤它们,但这看起来不切实际也不可行。我的想法是查询数据并返回一个指数级的小数组值,但我在使用提供的查询方法时遇到了问题。
如何查询特定方面我的数据库的结构?
如何构建我的代码,使其可行;没有加载每个用户,一次最多10个。
任何建议,资源和链接非常感谢。
编辑:
我做了一些研究,看起来Firebase带有一些内置的查询方法...到目前为止这是我试图用下面的代码来测试它打印出我开始用户,但我不能打印任何用户在控制台中
< ($ value):pre>
ref.queryOrderedByKey()。queryStarting(atValue:I)。queryEnding(atValue:I\\\\ {f8ff})
.observe(.childAdded, (快照在
print(snapshot.key)
})
有一些解决方案,并经常加载所有的用户数据是太多的数据。
这是一个典型的用户节点
pre code $ users
uid_0
名称:Jean Luc
uid_1
名称:Will
uid_2
名称:Geordi
一个选项是逐个遍历每个用户节点,以检索用户名。这完全避免了庞大的数据集。我们将使用.childAdded事件来加载每个事件并将其存储在一个数组中。
let usersRef = self.ref.child(用户)
$ p $第二个选择是将用户名存储在一个完全不同的节点中,这样可以大大减少杂乱,因为其余的数据仍然保留在主用户节点中。
var userNamesArray = [String]()
usersRef.observe(.childAdded,with:{snapshot in
let userDict = snapshot.value as![String:任何]
让name = userDict [name] as!String
userNamesArray.append(name)
})
user_names
uid_0:Jean Luc
uid_1:Will
uid_2:Geordi
正如你所看到的,即使有成千上万个名字, 。
另一种选择是一次加载X个用户,使用.startingAt和.endingAt并遍历r使用户得到每个名字。在这种情况下,我们希望所有用户以A开头,并以M结尾。对不起Worf。
let usersRef = self.ref .child(users)
var userNamesArray = [String]()
$ b $ let nameQuery = usersRef.queryOrdered(byChild:name)
.queryStarting(atValue: A)
.queryEnding(atValue:M\u {f8ff})
nameQuery.observe(.value,其中:$ snapshot中的
为snapshot.children中的子元素{
let snap = child as!DataSnapshot
let userDict = snap.value as![String:Any]
let name = userDict [name] as!String
userNamesArray.append (姓名)
})
最后一个例子是以用户名从A开始,以M +结尾的用户名结尾,表示所有以M开头的名字,该以上在查询中使用\字符是Unicode的范围内的非常高的代码点
。因为它在
Unicode中最常见的字符之后,查询匹配以queryString开头的所有值。
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.
How do I query a specific aspect of my database?
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.
EDIT:
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: "I\u{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"
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.
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: "M\u{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) } })
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
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屋!