I have been trying a lot of things and I have been searching a lot on the internet but I can't find a solution that helps me because of how my code is set up.
我一直在尝试很多东西,我一直在互联网上搜索很多,但由于我的代码设置方式,我无法找到帮助我的解决方案。
Any how I have been trying to delete posts but I don't really know how to do this since my posts are uploaded in a Auto Id generated by firebase and I dont know what to write here
任何我一直试图删除帖子但我真的不知道如何做到这一点,因为我的帖子上传了由firebase生成的自动ID,我不知道在这里写什么
Database.database().reference.child("posts").child("HERE IS THE AUTO ID").removeValue
How do I get this? Please help me I have been stuck for this problem a while now. I have no clue how to get the autoid.
我怎么得到这个?请帮帮我,我已经被困了一段时间了。我不知道如何获得autoid。
this is how I upload the post
这是我上传帖子的方式
if (self.imageFileName != "") {
if choosenCountryLabel.text == "Albania" {
// image has finshed the uploading, Saving Post!!!
if let uid = Auth.auth().currentUser?.uid {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
if let userDictiOnary= snapshot.value as? [String: AnyObject] {
for user in userDictionary{
if let username = user.value as? String {
if let streetAdress = self.locationAdressTextField.text {
if let title = self.titleTextField.text {
if let cOntent= self.contentTextView.text {
let postObject: Dictionary = [
"uid" : uid,
"title" : title,
"content" : content,
"username" : username,
"time" : self.timeStamps,
"timeorder" : self.secondTimeStamps,
"image" : self.imageFileName,
"adress" : streetAdress,
"postAutoID" : self.postAutoID
]
let postID = Database.database().reference().child("posts").childByAutoId()
let postID2 = Database.database().reference().child("AlbaniaPosts").childByAutoId()
let postID3 = Database.database().reference().child(uid).childByAutoId()
postID.setValue(postObject)
postID2.setValue(postObject)
postID3.setValue(postObject)
let postAutoID = postID.key
let postAutoID2 = postID2.key
let postAutoID3 = postID3.key
print(postAutoID)
print(postAutoID2)
print(postAutoID3)
let alertPosting = UIAlertController(title: "Successfull upload", message: "Your acty was successfully uploaded.", preferredStyle: .alert)
alertPosting.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
let vc = self.storyboard?.instantiateViewController(withIdentifier: "AlbaniaVC")
self.present(vc!, animated: true, completion: nil)
}))
self.present(alertPosting, animated: true, completion: nil)
print("Posted Succesfully to Firebase, Saving Post!!!")
}
}
}
}
}
}
})
}
}
}else{
let alertNotPosting = UIAlertController(title: "Seems like you got connection problems", message: "Your image has not been uploaded. Please Wait 10 seconds and try again.", preferredStyle: .alert)
alertNotPosting.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertNotPosting, animated: true, completion: nil)
}
0
Delete data
删除数据
The simplest way to delete data is to call remove() on a reference to the location of that data.
删除数据的最简单方法是在对该数据位置的引用上调用remove()。
You can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.
您还可以通过将null指定为另一个写操作(如set()或update())的值来删除。您可以将此技术与update()一起使用,以在单个API调用中删除多个子项。
0
You have to save the autoID when you query your data or you have to save the key inside the post when you upload it.
您在查询数据时必须保存autoID,或者在上传时必须将密钥保存在帖子中。
If want to get the key when you query. You can do something like this:
如果想在查询时获取密钥。你可以这样做:
let ref = Database.database().reference()
ref.child("posts").queryLimited(toLast: 7).observeSingleEvent(of: .value, with: { snap in
for child in snap.children {
let child = child as? DataSnapshot
if let key = child?.key { // save this value in your post object
if let post = child?.value as? [String: AnyObject] {
if let adress = post["adress"] as? String, let title = post["title"] as? String { // add the rest of your data
// create an object and store in your data array
}
}
}
}
})
Note the above query only gets the last 7 posts. If you want to continue to get more you'll need to look into pagination.
请注意,上述查询仅获取最近7个帖子。如果你想继续获得更多,你需要考虑分页。
If you want to save the id in your posts you just add it when you upload like this:
如果您想在帖子中保存ID,只需在上传时添加:
let key = ref.child("posts").childByAutoId().key
let post = ["adress": adress,
"content": content,
"postID": key] as [String: Any]
let postFeed = ["\(key)" : feed]
ref.child("posts").updateChildValues(postFeed, withCompletionBlock: { (error, success) in
if error != nil {
// report the error
}
else {
// everything is fine
}
})
Then when you query you can do something like this:
然后,当您查询时,您可以执行以下操作:
let ref = Database.database().reference()
ref.child("posts").observeSingleEvent(of: .value, with: { snap in
for child in snap.children {
if let post = child?.value as? [String: AnyObject] {
if let postID = post["postID"] as? String {
// save this key in a post object so you can access it later to delete
}
}
}
})
Now assuming you created an object called post you can delete that post using
现在假设您创建了一个名为post的对象,您可以使用删除该帖子
ref.child("posts").child(post.postID).removeValue(completionBlock: { (error, refer) in
if error != nil {
// failed to delete post
}
else {
// delete worked
}
})