我有问题。我正在尝试遍历一个字符串数组,其中当前用户所关注的用户ID在。
我的用户当前关注了两个用户,因此tableData
数组中有两个用户ID。问题在于以下代码它只显示其中一个用户帖子,而不是两者。它向用户显示我最近添加的帖子,而不是我关注的所有用户帖子。我怎么能改变它?
希望你明白我的意思。这是我的代码:
FIRDatabase.database().reference().child("Users").child(userID).child("Following").observeEventType(.ChildAdded, withBlock: { snapshot in
var tableData = [String]()
let title = snapshot.value!["Id"] as? String
tableData.append(title!)
print(tableData[0])
for i in 0..<tableData.count {
FIRDatabase.database().reference().child("feed-items").queryOrderedByChild("profilePhoto").queryEqualToValue(tableData[i]).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
var newUpdates = [Sweet]()
for update in snapshot.children {
let updateObject = Sweet(snapshot: update as! FIRDataSnapshot)
newUpdates.append(updateObject)
}
self.updates = newUpdates.reverse()
self.tableView.reloadData()
}) { (error: NSError) in
print(error.description)
}
}
})
我也试过这段代码,但现在每当用户点击时(也有类似按钮和评论功能),就好像它会再次附加所有相同的帖子,所以它会复制来自用户的所有帖子刚喜欢,这是新代码:
FIRDatabase.database().reference().child("Users").child(userID).child("Following").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
var newFollowers = [FollowersStruct]()
for updateFollowers in snapshot.children {
let updateObjectFollowers = FollowersStruct(snapshot: updateFollowers as! FIRDataSnapshot)
newFollowers.append(updateObjectFollowers)
}
self.followings = newFollowers
print(self.followings.count)
for i in 0..<self.followings.count {
let following = self.followings[i]
print(following)
FIRDatabase.database().reference().child("feed-items").queryOrderedByChild("profilePhoto").queryEqualToValue(following.Id).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
for update in snapshot.children {
let updateObject = Sweet(snapshot: update as! FIRDataSnapshot)
self.newUpdates.append(updateObject)
}
self.updates = self.newUpdates.reverse()
self.tableView.reloadData()
}) { (error: NSError) in
print(error.description)
}
}
}) { (error: NSError) in
print(error.description)
}
答案 0 :(得分:0)
尝试将您的查询更改为: -
FIRDatabase.database().reference().child("Users").child(userID).child("Following").observeEventType(.Value,...
此外,如果您正在制作集成以下的应用,请查看DataStructure的此答案:Data Structure
答案 1 :(得分:0)
。添加的Child仅在添加新数据时调用,并且仅返回快照中新添加的数据。
FIRDatabase.database()。reference()。child(“Users”)。child(userID).child(“Following”)。observeEventType(.Value,withBlock:{snapshot in var tableData = String 对于snapshot.children中的eachValue { 让title = eachValue![“Id”]为?串 tableData.append(标题!) 打印(资料表) }
for i in 0..<tableData.count {
FIRDatabase.database().reference().child("feed-items").queryOrderedByChild("profilePhoto").queryEqualToValue(tableData[i]).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
var newUpdates = [Sweet]()
for update in snapshot.children {
let updateObject = Sweet(snapshot: update as! FIRDataSnapshot)
newUpdates.append(updateObject)
}
self.updates = newUpdates.reverse()
self.tableView.reloadData()
}) { (error: NSError) in
print(error.description)
}
}
})