如何设计一个简单的 Firebase 数据库来存储数组? [英] How do I design a simple Firebase Database that stores arrays?
问题描述
我正在 Swift 中使用 Xcode 8.2.1 编写 IOS 应用程序.目前,我为一个视图控制器设置了数据库,该控制器实现了 GeoFire 并成功地向数据库添加了位置.在单独的视图控制器上,我想添加对同一数据库的引用并将子项添加到此引用.
I am in the midst of coding an IOS application using Xcode 8.2.1 in Swift. Currently, I have the database set up for one view controller that implements GeoFire and successfully adds locations to the database. On a separate view controller, I want to add a reference to the same database and add children to this reference.
这个单独的引用我想存储可以通过数组名访问的不同数组.我希望能够在数据库中添加和删除保存的数组.此外,我希望能够添加和删除保存在每个单独数组中的字符串.
This separate reference I want to store different arrays that can be accessed by the array name. I want to be able to add and remove the saved arrays in the database. In addition, I want to be able to add and remove the strings that are held in each individual array.
我试过的代码是:
let ref = FIRDatabase.database().reference()
let childRef = FIRDatabase.database().reference(withPath: "arrays")
let itemsRef = ref.child("arrays")
let milkRef = itemsRef.child("array1")
但是当我运行此代码时,数据库不会更新以显示任何新子项.有可能完成我的任务吗?如果是这样,我做错了什么?
But when I run this code, the database does not update to show any new children. Is it possible to accomplish my task? If so, what am I doing wrong?
如果我的问题有任何不清楚,请告诉我,我愿意详细说明.在此先感谢您的帮助!
If my question is unclear in any way please let me know and I will be willing to elaborate. Thank you in advance for any help!
推荐答案
阅读此内容:数组是邪恶的
通常最好避免使用它们,因为通常有更好的方法来组织您的数据.
In general it's best of avoid them as there are usually better ways to organize your data.
这里有一些信息:
这将创建一个名为 child_node 的节点并为其分配一个 some_value 的值
This creates a node called child_node and assigns it a value of some_value
let ref = FIRDatabase.database().reference()
let childRef = ref.child("child_node")
childRef.setValue("some_value")
结果
root
child_node: some_value
如果再次运行代码,它将覆盖现有节点.
If you run the code again, it will overwrite the existing node.
一般来说,最好将节点名称与其包含的数据分离,以便稍微扩展
In general it's best to disassociate nodes names from the data they contain so expanding a bit
let ref = FIRDatabase.database().reference()
let childRef = ref.childByAutoId()
childRef.setValue("some_value")
结果
root
-Y8hjj9a99js9jd: some_value
-Yi00ksomosooss: some_value //the second time it's run
然后走得更远
let ref = FIRDatabase.database().reference()
let childRef = ref.childByAutoId()
let someData = ["site": "Olympus Mons", "location": "Mars"]
childRef.setValue(someData)
结果
root
-Y88ujs9mskkms:
site: "Olympus Mons"
location: "Mars"
然后你问自己自己,这与数组有什么关系"?
So you then ask yourself 'self, what does this have to do with arrays'?
这是存储额外"数据的更好方法.例如,您可以在他们的节点中存储用户位置
It's a better way to store 'extra' data. So for example you could store a users location within their node
root
users
-Yi8joiomso (uid)
g: xxxx
l:
0: xxx
1: xxx
或保留对它们在另一个节点中位置的引用
or keep a reference to where they are in another node
root
users
-Yi8joiomso (uid)
location: -Yiuhj89ejee
location
-Yiuhj89ejee
g: xxxx
l:
0: xxx
1: xxx
或将用户保留在位置节点内
or keep the users within the location node
root
location
userID
g: xxxx
l:
0: xxxx
1: xxxx
非数组结构为查询、检索数据和组织数据提供了更大的灵活性.
Non-array structures give far more flexibility for queries, retrieving data and organizing data.
这篇关于如何设计一个简单的 Firebase 数据库来存储数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!