Ios 为什么它会覆盖数据而不是在firebase中添加更多数据?
我很想知道为什么用户输入新数据时会覆盖数据, 我希望它添加更多数据,而不是覆盖数据 还想知道怎么读吗 先谢谢你Ios 为什么它会覆盖数据而不是在firebase中添加更多数据?,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我很想知道为什么用户输入新数据时会覆盖数据, 我希望它添加更多数据,而不是覆盖数据 还想知道怎么读吗 先谢谢你 let oDB = Database.database().reference().child("Data") let oDictionary = ["Data1" : strange.text! , "Data2" : stranger.text!] let uid = Auth.auth().currentUser?.uid oDB.child(uid!)
let oDB = Database.database().reference().child("Data")
let oDictionary = ["Data1" : strange.text! , "Data2" : stranger.text!]
let uid = Auth.auth().currentUser?.uid
oDB.child(uid!).setValue(oDictionary) {
(error, reference) in
if error != nil{
print(error!)
} else {
print("saved Sucessfully")
self.navigationController?.popViewController(animated: true)
}
}
//In another ViewController
func updateRequest() {
let uid = Auth.auth().currentUser?.uid
let yDb = Database.database().reference().child("Data").child(uid!)
postDb.observeSingleEvent(of: .value) { (snapShot) in
if let snapShotValue = snapShot.value as? Dictionary<String, String> {
let text = snapShotValue["Data1"]!
let case = snapShotValue["Data2"]!
let data = Data()
data.s= text
data.y = case
self.array.append(data)
self.table.reloadData()
}
}
}
让oDB=Database.Database().reference().child(“数据”)
让oDictionary=[“Data1”:陌生的.text!,“Data2”:陌生的.text!]
设uid=Auth.Auth().currentUser?.uid
oDB.child(uid!).setValue(oDictionary){
(错误,参考)在
如果错误!=nil{
打印(错误!)
}否则{
打印(“成功保存”)
self.navigationController?.popViewController(动画:true)
}
}
//在另一个ViewController中
func updateRequest(){
设uid=Auth.Auth().currentUser?.uid
让yDb=Database.Database().reference().child(“数据”).child(uid!)
中的postDb.observeSingleEvent(of:.值){(快照)
如果让snapShotValue=snapShot.value作为字典{
让text=snapShotValue[“Data1”]!
让案例=快照值[“数据2”]!
让数据=数据()
data.s=文本
data.y=case
self.array.append(数据)
self.table.reloadData()
}
}
}
setValue
覆盖旧内容,您可能需要childByAutoId
oDB.child(uid!).childByAutoId().setValue(oDictionary) {
(error, reference) in
if error != nil{
print(error!)
} else {
print("saved Sucessfully")
self.navigationController?.popViewController(animated: true)
}
这将产生这种结构
Data
> uid
> someKey1 <<<< auto generated
Data1:"---"
Data2:"---"
> someKey2 <<<< auto generated
Data1:"---"
Data2:"---"
setValue
覆盖旧内容,您可能需要childByAutoId
oDB.child(uid!).childByAutoId().setValue(oDictionary) {
(error, reference) in
if error != nil{
print(error!)
} else {
print("saved Sucessfully")
self.navigationController?.popViewController(animated: true)
}
这将产生这种结构
Data
> uid
> someKey1 <<<< auto generated
Data1:"---"
Data2:"---"
> someKey2 <<<< auto generated
Data1:"---"
Data2:"---"
这是Json:“数据”:{“RxV0P21NHXZHHHHTJL3KZ0ZDXN1”:{“数据1”:“Hil”,“数据2”:“Hello”}}这是Json:“数据”:{“RxV0P21NHXZHHHHHTJL3KZ0ZDXN1”:{“数据1”:“Hil”,“数据2”:“Hello”}好吧,我忘了读它了,现在就编辑它数据()是一个由s和y组成的类,它是否会以其他方式影响它答案中的选项和另一个带有
Codable
无其他方式OK听起来不错,我把我的Xcode放进去了,它没有显示数据为什么?嗨,如果我想添加一个更深的步骤,意味着在someKey1中添加另一个键值对呢?好的,也读一读怎么样?我忘了说会立即编辑它数据()是一个s字符串和y字符串的类它会以其他方式影响它吗答案中的选项和另一个带有Codable
no other way的选项听起来不错,我输入了Xcode,但它没有显示数据为什么?嗨,如果我想添加一个更深的步骤,意味着在someKey1中添加另一个键值对,该怎么办?