前提
我使用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 userApi
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,因此,我希望您通过具体代码等告诉我。
答案 0 :(得分:0)
这是一个大胆的猜测,因为尚不清楚OP如何确定要删除的节点。
假设UI中显示了tableView,并且用户已刷入row_to_delete。我们还将假设我们有一个Post对象数组,这些对象具有快照中的'key'属性。
要删除帖子,请从dataSource数组检索帖子并获取它的密钥。然后构建对该ans removeValue的引用。
let postToDelete = postsArray[row_to_delete]
let postKey = postToDelete.key
let ref = your_firebase.child("posts").child(postKey)
ref.removeValue()