gpt4 book ai didi

ios - Swift Firebase 加载图片的次数太多

转载 作者:行者123 更新时间:2023-11-28 10:50:29 25 4
gpt4 key购买 nike

我正在尝试从 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

}



}

后 View Controller

  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.")
}


}

最佳答案

每次添加新帖子时,代码都会附加另一个执行的监听器,因此您将为附加的每个监听器附加一次描述和 downloadUrls。没有必要单独监听每个 child ,因为对它们的任何更改都将由原始 .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()
})

关于ios - Swift Firebase 加载图片的次数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46307261/

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