ios - 标记帖子时从 firebase 中删除一个 child

标签 ios swift firebase

当帖子获得超过 2 个标志时,我一直试图从 firebase 中删除该帖子。我已经尝试了很多方法,但我无法弄清楚。我有一些附有帮助的图片,将不胜感激! `

 // 4
        if poster.uid != User.current.uid {
            let flagAction = UIAlertAction(title: "Report as Inappropriate", style: .default) { _ in
                PostService.flag(post)

            /// addedflag

            let flaggedPostRef = Database.database().reference().child("flaggedPosts").child(postKey!)


            // 3
            var flaggedDict = ["text": post.textData,
                               "poster_uid": post.poster.uid,
                               "reporter_uid": User.current.uid]

            // 4
            flaggedPostRef.updateChildValues(flaggedDict)

            // 5
            let flagCountRef = flaggedPostRef.child("flag_count")
            flagCountRef.runTransactionBlock({ (mutableData) -> TransactionResult in


                let currentCount = mutableData.value as? Int ?? 0
                mutableData.value = currentCount

            if mutableData.value as! Int >= 1 {



                let uid = FIRAuth.auth()!.poster.uid!.uid

                // Remove the post from the DB
                ref.child("posts").child(postKey).removeValue { error in
                    if error != nil {
                        print("error \(error)")
                    }
                }
                postKey?.removeVolue()
                let timelinePostDict = ["poster_uid" : poster.uid]
                var updatedData: [String : Any] = ["timeline/\(poster.uid)/\(postKey)" : timelinePostDict]

                let postToBeDeleted = Database.database().reference().child("posts")
                updatedData["timeline/\(poster.uid)/\(postKey)"] = timelinePostDict
                updatedData["posts/\(poster.uid)/\(postKey)"] = postKey


                print("Delete case: mutableData.value = \(mutableData.value)")

            } else {
                print("Case not met. Either not equal to 2 or not able to cast as Integer type. The value of the casted in is \(mutableData.value as? Int)")
            }




                mutableData.value = currentCount + 1


                return TransactionResult.success(withValue: mutableData)
            })

` the code for flaging Friebase

最佳答案

.runTransactionBlock({.. 在特定节点附加/更新值之前被多次触发。你需要的是一个观察者:-

Database.database().reference().child("flaggedPosts").observe(.childChanged, with: {(Snapshot) in

        // Every time a child dictionary in the flaggedPosts node
        // would change you will receive that dictionary in this block
        // be it any change in any of the values in that postKey
        // Check the flagCount value and perform the action requisite... 

        print(Snapshot.value ?? "No value retrieved")



        if let snapDict = Snapshot.value as? NSDictionary{

            let count = snapDict["count"] as! Int
            let postID = snapDict["postKey"] as! String
            let posterUID = snapDict["poster_uid"] as! String

            if count > 1{

                print(postID)

                // You will only be pushed into this block only if your count value
                // is greater than 1 i.e 2

                // Delete postID in your database

                Database.database().reference().child("posts/\(posterUID)/\(postID)").removeValue(completionBlock: {(Err, ref) in

                    // Handle the error you recieve while deleting 
                    // the flagged post


                })
            }
        }

    }, withCancel: {(Error) in

        // Handle the error recieved while making 
        // a call to firebase

    })

鉴于此方法使用 .observe(.childChanged..,这是一个异步主动调用,其观察者不会从网络链接中删除。到您的数据库,每次您 child 被改变/更新......

关于ios - 标记帖子时从 firebase 中删除一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45778156/

相关文章:

ios - 将 AVFoundation 中的图像分配给 UIImageView 时,主视图中的 subview 崩溃

ios - 指定字符串时无法将 NSURL 类型的值转换为 NSString (Swift)

ios - 为什么我的金额数组没有返回任何内容?

ios - 如何将当前时间增加 5 分钟

swift - 是否可以在 Swift iOS 应用程序中设置所有字体的字体大小?

ios - 如何在 swift 上刷新所有 firebase 数据

ios - 为什么 NSLineBreakByWordWrapping 有时不起作用

swift 4 + Alamofire : parse array of custom struct

firebase - 您可以从 Firebase 实时数据库 key 获取时间戳吗?

ios - 获取身份验证失败 : invalid_token (audience was project ' ' but should have been project ' ' )