我试图从Firebase加载用户"帖子"(图片及其说明)。一切似乎一切正常,我在viewController中看到3个帖子但是当我将新图像上传到Firebase时,用户所有帖子的数量相乘,然后应用程序加载的不是4个而是8个帖子。为什么会这样?
这是我的快照
Snap (qUJAq6aeNGYUeSsl9RmzldNpTME3) {
"508D22DE-E865-460E-A011-A9170E0AB6A8" = {
description = n;
downloadURL = "https://firebasestorage.googleapis.com/v0/b/findit-2f6eb.appspot.com/o/images%2F1A18AFAD-815C-417C-8C7A-226343FD0256.jpg?alt=media&token=46a6c929-a5ec-49e4-b6ca-aca896618fe0";
};
"D8434D58-1193-421D-8DFE-982983CB20D4" = {
description = Maaa;
downloadURL = "https://firebasestorage.googleapis.com/v0/b/findit-2f6eb.appspot.com/o/images%2F059C91E8-95A8-44C6-9779-F678E2B9E033.jpg?alt=media&token=46ff5654-abec-49dc-bbb8-afae0eb6f1bd";
};
"F0E47F44-AE22-4D80-B173-E2168CB20B94" = {
description = 7;
downloadURL = "https://firebasestorage.googleapis.com/v0/b/findit-2f6eb.appspot.com/o/images%2F1EA82DCA-FFE2-4B79-931C-28B66C7729AB.jpg?alt=media&token=8eaa2e02-5af8-4b43-adf2-2ac9214f902e";
};
}
MyPostsTableViewController
import UIKit
import Firebase
class MyPostsTableViewController: UITableViewController {
let user = Userr()
var downloadURL = ""
var desCription = ""
var allPosts = [String]()
let currUser = Auth.auth().currentUser?.uid
override func viewDidLoad() {
super.viewDidLoad()
let ref = Database.database().reference().child("posts").child(currUser!)
ref.observe(.value, with: { (snapshot) in
print(snapshot)
let dictionary = snapshot.value as! [String: Any]
var keys = Array(dictionary.keys)
self.allPosts = keys
for index in 0...(keys.count - 1) {
self.allPosts.removeAll()
self.user.descriptions.removeAll()
self.user.downloadUrls.removeAll()
ref.child(keys[index]).child("description").observe( .value, with: { (snapshot) in
ref.child(keys[index]).child("downloadURL").observe(.value, with: { (snapshot2) in
self.user.descriptions.append(snapshot.value as! String)
self.user.downloadUrls.append(snapshot2.value as! String)
self.tableView.reloadData()
})
})
}
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return user.descriptions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!MyPostsTableViewCell
// print("downloadUrls Count \(user.downloadUrls.count)")
cell.descriptionLabel.text = user.descriptions[indexPath.row]
let profileImageURL = user.downloadUrls[indexPath.row]
cell.imageVieeew.contentMode = .scaleAspectFit
cell.imageVieeew.loadImageUsingCacheWithUrlString(profileImageURL)
return cell
}
}
PostViewController
import UIKit
import Firebase
class PostViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let user = Userr()
@IBOutlet var textField: UITextField!
@IBAction func postToServer(_ sender: Any) {
self.user.allow = false
// successfully authenticated user
if imageViw.image != nil && textField.text != ""{
let imageName = NSUUID().uuidString
let postName = NSUUID().uuidString
let imagesFolder = Storage.storage().reference().child("images")
if let imageData = UIImageJPEGRepresentation(imageViw.image!, 0.2){
imagesFolder.child("\(imageName).jpg").putData(imageData, metadata: nil, completion:{
(metadata, error) in
if let error = error{
self.presentAlert(alert: error.localizedDescription)
} else{
if let downloadURL = metadata?.downloadURL()?.absoluteString {
let currUser = Auth.auth().currentUser?.uid
let ref = Database.database().reference().child("posts").child(currUser!).child(postName)
ref.child("downloadURL").setValue(downloadURL)
ref.child("description").setValue(self.textField.text!)
}
}
})
}
}
else {
// we're missing something
presentAlert(alert: "You mus provide an image and a message for your post.")
}
}
答案 0 :(得分:3)
每次添加新帖子时,代码都会附加另一个执行的侦听器,因此您将为每个已连接的侦听器附加描述并下载一次。没有必要单独监听每个孩子,因为对于他们的任何更改都将由原始.observe
侦听器触发,因为路径的.value
已更改。相反,遍历childSnapshots并根据需要附加数据。它看起来像这样,虽然我没有用你的数据和代码测试它,所以你可能不得不调整它。
let ref = Database.database().reference().child("posts").child(currUser!)
ref.observe(.value, with: { (snapshot) in
print(snapshot)
self.allPosts.removeAll()
self.user.descriptions.removeAll()
self.user.downloadUrls.removeAll()
guard let snapshots = snapshot.children.allObjects as? [DataSnapshot] else { return }
for snap in snapshots {
self.user.descriptions.append(snap.childSnapshot(forPath: "description").value as! String)
self.user.downloadUrls.append(snap.childSnapshot(forPath: "downloadURL").value as! String)
}
self.tableView.reloadData()
})
答案 1 :(得分:0)
问题在于/Illuminate/Foundation/Auth/RedirectsUsers
ref.observe(.value, with: { (snapshot) in
会导致返回所有数据。因此,您将获得重复的数据。将其更改为.value
,只有新的帖子信息会返回到您的应用。
同样在某些时候你可能想要删除那个观察者。由于我不知道你的应用是如何运作的,我不确定它会是什么时候,但要注意这一点。
使用.childAdded
来做到这一点。
了解获取数据的其他方式here