gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:03:46 25 4
gpt4 key购买 nike

当帖子获得超过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com