0
コレクションビューをリフレッシュすると、データは+1で+1されます。この機能を更新するためにプルするときに、配列の重複したエントリを避けるにはどうすればよいですか? self.posts.removeAll()もまだ使用されていません。溶液で更新Firebase observeSingleEventが連続して複写するエントリを配列に追加する
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
}
}
。
データを配列に追加した後、このself.posts.sort(by:{$ 0.date!> $ 1.date!})のような日付でソートすることができます。しかし、コレクションビューをリロードするためにスクロールするときに重複を避けるためには、依然として助けが必要です。ありがとう –
私の答えはあなたの問題を解決しない場合は、あなたのコードのいくつかが完全な意味をなさないために表示されていないので、あなたの全体のビューコントローラをアップロードする必要があります。 –
完全なコードを追加しました。ありがとう –