Firebase에서 사용자 "게시물"(이미지 및 설명)을로드하려고합니다. 처음에는 모든 것이 잘 작동하는 것처럼 보였고 viewController에 3 개의 게시물을 보았습니다.하지만 Firebase에 새로운 이미지를 업로드하면 사용자가 모든 게시물의 양을 늘린 다음 앱이 4 개가 아닌 8 개의 게시물을로드합니다. 왜 이런 일이 일어나는 걸까요?Swift Firebase가 이미지를 너무 많이로드합니다.
여기 내 스냅 샷
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.")
}
}
firebase만이 새 하위를 제공하는'Child added observer'를 추가하십시오. 'Type value observer'를 추가하면 모든 자식을 반환합니다. –