[英]ios - Firebase writing complex data
Hi I'm new to Swift development and just started using Google's firebase database. 嗨,我是Swift开发的新手,刚刚开始使用Google的firebase数据库。 I'm having some trouble constructing the JSON tree the way I want to, firstly because I'm using the .childByAutoId()
func that creates a unique ID, which works great for now, but what if I wanted to name it to "Locations" in the db? 我在以自己想要的方式构造JSON树时遇到了一些麻烦,首先是因为我使用的是.childByAutoId()
函数,该函数创建了一个唯一的ID,该ID现在很有效,但是如果我想将其命名为“数据库中的“位置”?
Secondly, my real issue is that I would like to have a nested array of Links
inside each of my Locations
, comprised of 2 strings. 其次,我真正的问题是我希望在每个Locations
中包含一个由2个字符串组成的嵌套Links
数组。 So I've tried using tuples but Firebase
didn't like that. 因此,我尝试使用元组,但是Firebase
不喜欢这样。 Was probably a silly solution. 可能是一个愚蠢的解决方案。 Anyhow I looked up firebase documentation and ended up with a bunch of compiling errors when trying to use it... 无论如何,我查阅了Firebase文档并在尝试使用Firebase文档时遇到了一系列编译错误...
What would be the correct syntax for constructing a nested "Links" array in each of my "Locations", or basically an efficient way for creating a complex JSON in firebase. 在我的每个“位置”中构造嵌套“链接”数组的正确语法是什么,或者基本上是在firebase中创建复杂JSON的有效方法。 Here's my experimental code: 这是我的实验代码:
class func pushToFirebase(){
let latitude = "33.333333"
let longitude = "33.333333"
let title = "foobar"
let link = ("linkTitle","linkUrl")
let link1 = "link"
let links = [link, link1]
let post:[String : AnyObject] = [
"latitude":latitude,
"longitude":longitude,
"title":title,
"links":links
]
let firebaseRef = FIRDatabase.database().reference()
firebaseRef.child("Locations").childByAutoId().setValue(post)
}
Always avoid using Tuples
or Arrays
in firebase database.Prefer using Dictionary
To create your desired JSON tree :- 始终避免在firebase数据库中使用Tuples
或Arrays
。首选使用Dictionary
创建所需的JSON树:-
class func pushToFirebase(){
let latitude = "33.333333"
let longitude = "33.333333"
let title = "foobar"
let link = ("linkTitle","linkUrl")
let link1 = "link"
let links = [link, link1]
let post:[String : AnyObject] = [
"latitude":latitude,
"longitude":longitude,
"title":title,
"links":links
]
//To create Locations
let firebaseRef = FIRDatabase.database().reference()
firebaseRef.child("Locations").childByAutoId().setValue(post)
//To create create Links
func createLinks(linkTitle : String!,linkURL : String!){
let selfUniqueGeneratedId = String(Int(NSDate.timeIntervalSinceReferenceDate() * 1000))
//Will give you a unique id every second it is called (or maybe millisecond too)
firebaseRef.child("Locations").child("Links").child(selfUniqueGeneratedId).setValue([link_title : linkTitle,
link_url : linkURL ])
}
}
You can not control Locations
objects since they are created with childByAutoId
that's why it has ..Auto..
in it... ;) 您无法控制Locations
对象,因为它们是使用childByAutoId
创建的,这就是为什么它具有..Auto..
的原因...;)
But surely you can read those AutoId
with this code.... 但是可以肯定的是,您可以使用此代码读取这些AutoId
。
func retrieveFirebaseDB(){
firebaseRef.child("Locations").observeEventType(.Value, withBlock: {(Snapshot) in
if Snapshot.exists(){
if let locationDict = Snapshot!.value as! [String:AnyObject] {
for each in locationDict as [String:AnyObject]{
let locationID = each.0
//Here you are retrieving every locationID in your node Locations, GO CRAZY...!
}
}
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.