【发布时间】:2018-10-25 03:03:56
【问题描述】:
我想从我的 Firebase 数据库中加载我当前用户的帖子。
这是我的数据库架构: 我已经成功地将所有帖子加载到主视图中,但是当我尝试复制并在此处使用相同的方法进行必要的更改并为当前用户的帖子使用 uid 时,它没有显示任何内容......已经没有想法了至于问题可能是什么,可以使用一些帮助。
这是来自 ProfileUserPosts swift 文件的屏幕截图:
//VARS
var postsuser = [ProfileUserPosts]()
@objc func observeUserPosts() {
let uid = Auth.auth().currentUser?.uid
let postsRef = Database.database().reference().child("posts").child("author")
postsRef.queryOrdered(byChild: "userid").queryEqual(toValue: uid!).observe(.value) { (snapshot) in
var tempPost = [ProfileUserPosts]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot {
let dict = childSnapshot.value as? [String: Any]
//Post Picture
let photoUrl = dict!["photoUrl"] as? String
let url = URL(string: photoUrl!)
//Info Post
let comments = dict!["comments"] as? String
let city = dict!["city"] as? String
let municipality = dict!["municipality"] as? String
let breed = dict!["breed"] as? String
let phoneuser = dict!["phone"] as? String
let postType = dict!["postType"] as? String
let petType = dict!["petType"] as? String
let gender = dict!["gender"] as? String
let timestampadoption = dict!["timestamp"] as? Double
let date = Date(timeIntervalSince1970: timestampadoption!/1000)
let post = ProfileUserPosts(breed: breed!, phone: phoneuser!, photoUrl: url!, city: city!, municipality: municipality!, petType: petType!, gender: gender!, timestamp: date, postType: postType!, comments: comments!)
tempPost.insert(post, at: 0)
}
DispatchQueue.main.async {
self.postsuser = tempPost
self.postsCollectionView.reloadData()
self.refresher.endRefreshing()
}
}
}
}
这里是我用 numberOfItemsInSection 和 cellForItem 加载 CollectionView 的地方
extension ProfileViewController: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return postsuser.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell
cell.set(post: postsuser[indexPath.row])
return cell
}
}
【问题讨论】:
标签: swift firebase firebase-realtime-database