gpt4 book ai didi

iphone - 我想添加一个删除帖子按钮

转载 作者:行者123 更新时间:2023-11-28 07:42:16 24 4
gpt4 key购买 nike

前提

我用firebase管理帖子,但是无法实现发帖删除功能。

代码Swift4 postApi

    import Foundation
   import FirebaseDatabase

class PostApi {
var REF_POSTS = Database.database().reference().child("posts")
var REF_MY_POSTS = Database.database().reference().child("myPosts")

static var shared: PostApi = PostApi()
private init() {
}

func observePosts(completion: @escaping (PostModel) -> Void) {
REF_POSTS.observe(.childAdded) { (snapshot) in
guard let dic = snapshot.value as? [String: Any] else { return }
let newPost = PostModel(dictionary: dic, key: snapshot.key)
completion(newPost)
}
}

func observePost(withPostId id: String, completion: @escaping (PostModel) -> Void ) {
REF_POSTS.child(id).observeSingleEvent(of: .value) { (snapshot) in
guard let dic = snapshot.value as? [String : Any] else { return }
let newPost = PostModel(dictionary: dic, key: snapshot.key)
completion(newPost)
}
}

func observeMyPosts(withUid uid: String, completion: @escaping (String) -> Void) {
REF_MY_POSTS.child(uid).observe(.childAdded) { (snapshot) in
let postId = snapshot.key
completion(postId)
}
}

func fetchCountPosts(withUid uid: String, completion: @escaping(UInt) -> Void) {
REF_MY_POSTS.child(uid).observe(.value) { (snapshot) in
let postCount = snapshot.childrenCount
completion(postCount)
}
}

func observeLike(withPostId id: String, completion: @escaping (Int) -> Void) {
REF_POSTS.child(id).observe(.childAdded) { (snapshot) in
guard let value = snapshot.value as? Int else { return }
completion(value)
}
}


guard let dic = snapshot?.value as? [String: Any] else { return }
guard let postId = snapshot?.key else { return }
let updatedPost = PostModel(dictionary: dic, key: postId)
onSuccess(updatedPost)
}
}

// 投稿を読み込んで好きな順に並べ替える
func observeTopPost(completion: @escaping(PostModel) -> Void) {
REF_POSTS.queryOrdered(byChild: "likeCount").observeSingleEvent(of: .value) { (snapshot) in
let arraySnapshot = (snapshot.children.allObjects as! [DataSnapshot]).reversed()

arraySnapshot.forEach({ (child) in
guard let dic = child.value as? [String: Any] else { return }
let post = PostModel(dictionary: dic, key: snapshot.key)
completion(post)
})
}
}

代码Swift4 用户接口(interface)

import Foundation
import FirebaseDatabase
import FirebaseAuth

class UserApi {
// Adresse Datenbank users
var REF_USERS = Database.database().reference().child("users")

// Singleton pattern (Einzelstück Muster)
static var shared: UserApi = UserApi()
private init() {
}

// Aktuell eingeloggte User ID
var CURRENT_USER_UID: String? {
if let currentUserUid = Auth.auth().currentUser?.uid {
return currentUserUid
}
return nil
}

// Aktuell eingeloggte User
var CURRENT_USER: User? {
if let currentUserUid = Auth.auth().currentUser {
return currentUserUid
}
return nil
}

// Lade User mit der id
func observeUser(uid: String, completion: @escaping (UserModel) -> Void) {
REF_USERS.child(uid).observeSingleEvent(of: .value) { (snapshot) in
guard let dic = snapshot.value as? [String: Any] else { return }
let newUser = UserModel(dictionary: dic)
completion(newUser)
}
}

// Lade user ohne id
func observeUser(completion: @escaping (UserModel) -> Void ) {
REF_USERS.observe(.childAdded) { (snapshot) in
guard let dic = snapshot.value as? [String: Any] else { return }
let user = UserModel(dictionary: dic)
completion(user)
}
}

// Lade den aktuellen User
func observeCurrentUser(completion: @escaping (UserModel) -> Void ) {
guard let currentUserUid = CURRENT_USER_UID else { return }
REF_USERS.child(currentUserUid).observeSingleEvent(of: .value) { (snapshot) in
guard let dic = snapshot.value as? [String: Any] else { return }
let currentUser = UserModel(dictionary: dic)
completion(currentUser)
}
}

// User im Suchfeld laden
func queryUser(withText text: String, completion: @escaping(UserModel) -> Void ) {
REF_USERS.queryOrdered(byChild: "username_lowercase").queryStarting(atValue: text).queryEnding(atValue: text + "\u{f8ff}").queryLimited(toLast: 5).observeSingleEvent(of: .value) { (snapshot) in
snapshot.children.forEach({ (data) in
let child = data as! DataSnapshot
guard let dic = child.value as? [String: Any] else { return }
let user = UserModel(dictionary: dic)
completion(user)
})
}
}
}

补充信息

如果我们有其他缺失信息,我们将添加其他信息。由于我经常在4周内不理解Swift,我希望你用具体的代码等告诉我。

引用资料 How to delete a child from Firebase (Swift)

最佳答案

这是对答案的大胆猜测,因为尚不清楚 OP 如何确定要删除的节点。

让我们假设一个 tableView 显示在 UI 中并且用户滑动了一个 row_to_delete。我们还将假设我们有一组 Post 对象,这些对象具有来自快照的“key”属性。

然后要删除帖子,请从数据源数组中检索帖子并获取它的键。然后为该 ans removeValue 构建一个引用。

let postToDelete = postsArray[row_to_delete]
let postKey = postToDelete.key
let ref = your_firebase.child("posts").child(postKey)
ref.removeValue()

关于iphone - 我想添加一个删除帖子按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51980046/

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