gpt4 book ai didi

swift - 仅从 firebase 检索喜欢的帖子

转载 作者:行者123 更新时间:2023-11-28 15:35:59 25 4
gpt4 key购买 nike

我只想从 firebase 数据库中检索我喜欢的帖子。

下面是我的数据。我希望这个喜欢帖子的用户仅在该用户喜欢该帖子时才显示该帖子。

我已经在从 firebase 中检索数据,但它会显示所有帖子,而不仅仅是喜欢的帖子。

enter image description here

下面的代码是我正在使用的代码,它的工作原理是,如果我点击喜欢它会显示数据,如果我不喜欢则不会显示数据,但这是显示所有数据。

        DataService.ds.REF_USER_CURRENT.observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.hasChild("favourite") {

print("true Favourits exist")


DataService.ds.REF_POSTS.observe(DataEventType.value, with: { (snapshot) in
self.posts = []

if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
print("SNAP: \(snap)")
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
}
}
self.tableView.reloadData()

})
}else {
print("Tony: Couldnt get the data")
}

})
}

从这里用 VLADS ANSWER 编辑>>>>>>>

DataService.ds.REF_USER_CURRENT.observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.hasChild("favourite") {


let refToUser = DataService.ds.REF_USER_CURRENT // <- your userId
refToUser.child("favourite").observeSingleEvent(of: .value, with: { snapshot in
let value = snapshot.value as? NSDictionary
let postsIds = value?.allKeys as! [String]

for postId in postsIds {
let refToPost = Database.database().reference(withPath: "posts/" + postId)
refToPost.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists() {
self.notAddedLabel.isHidden = true
print("true Favourits exist")


// refToPost.observe(DataEventType.value, with: { (snapshot) in
self.posts = []

if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
print("SNAP: \(snap)")
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
}
}
self.tableView.reloadData()

// })
}else {
self.notAddedLabel.isHidden = false
print("Tony: Couldn't get the data")
}
})
}
})
}else {
print("Tony: No Favs added, couldn't get the data")
}
})
}

enter image description here

最佳答案

第一步: 转到收藏夹 节点并获取所有帖子 ID:

let refToUser = FIRDatabase.database().reference(withPath: "Users").child(userId) // <- your userId
refToUser.child("favourite").observeSingleEvent(of: .value, with: { snapshot in
if let value = snapshot.value as? NSDictionary
let postsIds = value.allKeys as! [String]

// Second step: fetch every post
for postId in postsIds {
let refToPost = FIRDatabase.database().reference(withPath: "posts/" + postId)
refToPost.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists() {
// do whatever you want
// snapshot now is your post data
} else {
print("error")
}
})
}

因此,您的主要问题是您引用的是所有帖子,而不是特定帖子。

希望你明白了。

关于swift - 仅从 firebase 检索喜欢的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268552/

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