gpt4 book ai didi

swift - Firebase observeSingleEvent 连续复制条目附加在我的数组中

转载 作者:行者123 更新时间:2023-11-28 15:39:33 27 4
gpt4 key购买 nike

当我刷新 Collection View 时,我的数据重复 +1。当我拉动刷新此功能时,如何避免数组中出现重复条目​​?也用了 self.posts.removeAll() 还是没有结果。

var posts = [Post]() {
didSet {
collectionView?.reloadData()
}
}

var following = [String]()
let refreshControl = UIRefreshControl()

override func viewDidLoad() {
super.viewDidLoad()

refreshControl.tintColor = UIColor.gray
refreshControl.addTarget(self, action: #selector(fetchPosts), for: UIControlEvents.valueChanged)
collectionView?.addSubview(refreshControl)
collectionView?.alwaysBounceVertical = true
fetchPosts()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.collectionView.reloadData()
}

func fetchPosts(){
let ref = FIRDatabase.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
guard let users = snapshot.value as? [String : AnyObject] else {
return
}
print(snapshot.key)
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid == FIRAuth.auth()?.currentUser?.uid {
if let followingUsers = value["following"] as? [String : String]{
for (_,user) in followingUsers{
self.following.append(user)
print(user)
}
}
self.following.append(FIRAuth.auth()!.currentUser!.uid)

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
let post = postSnapshot.value as! [String : AnyObject]
print(snap.key)

if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {
print(each)
let posst = Post()

if let date = post["date"] as? Int, let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {

posst.date = Int(date)
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID

print(posst)
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_,person) in people {
posst.peopleWhoLike.append(person as! String)
}
}


var postExist:Bool = false
for post in self.posts {
if post.postID == posst.postID {
postExist = true
break
}
}

if !postExist {
self.posts.append(posst)
}

self.posts.sort(by: {$0.date! > $1.date!})
self.refreshControl.endRefreshing()
}
}
}
self.collectionView.reloadData()
}
}
})
}
}
}

})
ref.removeAllObservers()
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostCell.identifier, for: indexPath) as! PostCell

cell.posts = posts[indexPath.row]

let post = posts[indexPath.row]
for person in post.peopleWhoLike {
if person == FIRAuth.auth()!.currentUser!.uid {
cell.like.isHidden = true
cell.unlike.isHidden = false
break
}
}
return cell
}

更新了解决方案。

最佳答案

我认为当刷新被激活时,这个函数被调用并且所有的帖子都被下载了。对于重复的问题,您似乎一直在将数据附加到 posts 数组而不删除旧数据。

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in

let postsSnap = snap.value as! [String : AnyObject]

self.posts.removeAll() // This will remove previously downloaded posts.

// All your other code ...

关于swift - Firebase observeSingleEvent 连续复制条目附加在我的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898815/

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