如何从firebase中删除帖子?迅速 [英] how to delete post from firebase? Swift

查看:50
本文介绍了如何从firebase中删除帖子?迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试很多事情,并且在互联网上进行了大量搜索,但是由于代码的设置方式,我找不到能帮助我的解决方案.

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.

任何我一直试图删除帖子的方法,但是我真的不知道该怎么做,因为我的帖子是通过firebase生成的自动ID上传的,我也不知道在这里写些什么

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

Database.database().reference.child("posts").child("HERE IS THE AUTO ID").removeValue

我怎么得到这个?请帮助我,我已经为这个问题困扰了一段时间.我不知道如何获得autoid.

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.

这就是我上传帖子的方式

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<String, Any> = [
                                                "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)
    } 

推荐答案

在查询数据时必须保存自动ID,或者在上传信息时必须将其保存在帖子中.

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.

如果要在查询时获取密钥.您可以执行以下操作:

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
                }            
            }
        }
    }
})

请注意,以上查询仅获取最近的7条帖子.如果您想继续获得更多,则需要研究分页.

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.

如果要将ID保存在帖子中,只需在上传时将其添加即可,如下所示:

If you want to save the id in your posts you just add it when you upload like this:

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
            }
        }
    }
})

现在假设您创建了一个名为post的对象,则可以使用以下内容删除该帖子

Now assuming you created an object called post you can delete that post using

ref.child("posts").child(post.postID).removeValue(completionBlock: { (error, refer) in
    if error != nil {
        // failed to delete post                    
    }
    else {
        // delete worked
    }
})

这篇关于如何从firebase中删除帖子?迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆