添加Firebase帖子会覆盖所有其他帖子Swift [英] Adding Firebase Post Overwrites All Other Posts Swift
本文介绍了添加Firebase帖子会覆盖所有其他帖子Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了将一篇文章正确地写到Firebase数据库中的代码.问题是,当我添加另一个帖子时,它会覆盖之前的帖子.如何在不覆盖的情况下将帖子添加到现有数组?
I have written code that correctly writes a single post to my Firebase database. The issue is that when I add another post it overwrites the post that was there previously. How can I add the posts to the existing array without overwriting?
func postToFirebase(imgUrl: String) {
let post: Dictionary<String, AnyObject> = [
"caption": postDescription.text! as AnyObject,
"imageUrl": imgUrl as AnyObject,
"likes": 0 as AnyObject
]
let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
firebasePost.setValue(post)
let userPost = firebasePost.key
print("Firebase Post: \(String(describing: firebasePost))")
_ = Auth.auth().addStateDidChangeListener { (auth,user) in
if let user = user {
let userId = user.uid
print("USER: \(String(describing: userId))")
let newPost = DataService.ds.REF_USERS.child("\(userId)").child("posts")
//print("NEW POST: \(newPost.child)")
newPost.setValue([userPost : true])
}
}
postDescription.text = ""
imageSelected = false
newPostImage.image = UIImage(named: "icons8-camera-100")
}
推荐答案
我可以通过对设置值的方式进行一些小的更改来解决此问题.现在看起来像这样:
I was able to resolve the issue by making a minor change to how I was setting the value. It now looks like this:
let newPost = DataService.ds.REF_USERS.child("\(userId)").child("posts").child(userPost!)
newPost.setValue(true)
这篇关于添加Firebase帖子会覆盖所有其他帖子Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文