gpt4 book ai didi

ios - 从 Firebase 下载表格 View 中的项目过多

转载 作者:行者123 更新时间:2023-11-30 12:32:09 30 4
gpt4 key购买 nike

当我从不同的设备上发帖时,当前设备上的表格 View 会保留以前存在的项目,然后再次下载这些项目,包括新帖子,任何人都可以帮助我制作它,以便它只显示其中之一每一个项目?

我认为问题出在我的 downloadFromFirebase() 中。

这是我的代码:

class DisplayVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var captionField: RoundTextField!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var myMainImg: roundImage!

var imageSelected = false
static var imageCache: NSCache<NSString,UIImage> = NSCache()
var posts = [Post]() // array for the posts
var imagePicker: UIImagePickerController!

@IBAction func addImageTapped(_ sender: Any) {
present(imagePicker, animated: true, completion: nil)

}

@IBAction func logoutPressed(_ sender: Any) {
//remove keychain and sign out of firebase
let keychainResult = KeychainWrapper.standard.removeObject(forKey: KEY_UID)
print("AA: ID removed from keychain: \(keychainResult)")
try! FIRAuth.auth()?.signOut()
performSegue(withIdentifier: "goToSignIn", sender: nil)

}
@IBAction func postBtnTapped(_ sender: Any) {
//does this exist? or is it null? if condition is not true then it is executed
guard let caption = captionField.text, caption != "" else {
let alert = UIAlertController(title: "Bad Caption", message: "Caption must be entered", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
guard let img = myMainImg.image, imageSelected == true else {
let alert = UIAlertController(title: "Bad Image", message: "Image must be choosen", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return

}
if let imgData = UIImageJPEGRepresentation(img, 0.2){

let imgUid = NSUUID().uuidString
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"

DataService.ds.REF_POST_IMAGES.child(imgUid).put(imgData, metadata: metadata, completion: { (metadata, error) in
if error != nil{
print("unable to upload image to Firebase storage")
}else{
print("GREAT SUCESS FOR IMAGE ON STORAGE")
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL{
self.postToFireBase(imageURL: url)
}
}
})
}

}

func postToFireBase(imageURL: String){
let post: Dictionary<String, AnyObject> = ["caption": captionField.text as AnyObject,"imageURL":imageURL as AnyObject, "likes":0 as AnyObject,"userName": userName as AnyObject]

let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
firebasePost.setValue(post)

captionField.text = ""
imageSelected = false
myMainImg.image = UIImage(named: "add-image")

posts.removeAll()
tableView.reloadData()
}



//cannot use the view did load for the guard method!
override func viewDidAppear(_ animated: Bool) {

}

override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()

tableView.delegate = self
tableView.dataSource = self

imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self

posts.removeAll()
self.downloadFromFirebase()
}

func downloadFromFirebase(){
//"POSTS" Listener, initialize listener and it will work constantly
DataService.ds.REF_POSTS.observe(.value, with: { (snapshot) in

if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot]{
for snap in snapshot{
print("SNAP: \(snap)") // make free objects by parsing the JSON
if let postDict = snap.value as? Dictionary<String, AnyObject>{
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post) //stick the post in the posts Array
}

}
}
self.tableView.reloadData()
})
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count //number of total posts
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]

if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell{

if let img = DisplayVC.imageCache.object(forKey: post.imageURL as NSString){
cell.configureCell(post: post, image: img)
return cell
}else{
cell.configureCell(post: post, image: nil)
return cell
}
}else{
return PostCell()
}

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage{
myMainImg.image = image

}else{
print("AA: Valid image wasn't selected")
}
imagePicker.dismiss(animated: true, completion: nil)
imageSelected = true

}

func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
view.endEditing(true)
}
}

最佳答案

试试这个:

包含在之后

let post = Post(postKey: key, postData: postDict)

以下内容:

if !posts.contains(post) {
self.post.append(post)
}

让我知道它是否有效!

关于ios - 从 Firebase 下载表格 View 中的项目过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381770/

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