我有一段将数据保存到Firebase的工作代码:
let locRef = locationRef.childByAutoId()
let locItem = [
senderId : [
"location": getLocationID()
]
]
locRef.setValue(locItem)
我想要检索用户(由senderID标识)" location",所以我尝试了这段代码:
locationRef.child("location").child(senderId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
self.locationId = snapshot.value!["location"] as! String
}) { (error) in
print(error.localizedDescription)
}
然而,当运行代码时,我的应用程序崩溃,并且没有错误。我认为我的错误可能是.child("location")
必须是别的,但我不知道是什么。
数据库结构(JSON):
{
"locations" : {
"-KLEdoj2eiF7EW9m0815" : {
"W6vSOHZLTwNM33JYqkKHvaIVRF13" : {
"location" : "Seattle, WA"
}
},
"-KLLfcOvYHwIufBALM0-" : {
"W6vSOHZLTwNM33JYqkKHvaIVRF13" : {
"location" : "London, United Kingdom"
}
},
任何帮助将不胜感激!
答案 0 :(得分:0)
您应该将您的位置称为以下
FIRDatabase.database().reference().child("locations").child(locationId).child(senderId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
self.location = snapshot.value!["location"] as! String
}
例如:
如果您有locationId = "-KLEdoj2eiF7EW9m0815"
和senderId = "W6vSOHZLTwNM33JYqkKHvaIVRF13"
,则上述通话会将self.location
设为"Seattle, WA"