Wenn die Seite zu aktualisieren erhalte ich die FehlerIndex außerhalb des zulässigen Bereichs für Array von Objekten
fatal error: Index out of range
ich den Fehler posts.removeAll()
durch Löschen der Linie lösen kann, aber dann bekomme ich mehrere Zellen des gleichen Typs.
und ich kann nichts arbeiten aus allen anderen Lösungen auf Stack-Überlauf für mein Szenario
ich Hilfe schätzen würde
import UIKit
import Firebase
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, HomePostCellDelegate {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(HomePostCell.self, forCellWithReuseIdentifier: cellId)
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
collectionView?.refreshControl = refreshControl
fetchAllPosts()
}
func handleUpdateFeed() {
handleRefresh()
}
func handleRefresh() {
print("Handling refresh..")
posts.removeAll()
fetchAllPosts()
}
fileprivate func fetchAllPosts() {
fetchPosts()
fetchFollowingUserIds()
}
fileprivate func fetchFollowingUserIds() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.database().reference().child("following").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
userIdsDictionary.forEach({ (key, value) in
Database.fetchUserWithUID(uid: key, completion: { (user) in
self.fetchPostsWithUser(user: user)
})
})
}) { (err) in
print("Failed to fetch following user ids:", err)
}
}
var posts = [Post]()
fileprivate func fetchPosts() {
guard let uid = Auth.auth().currentUser?.uid else { return }
Database.fetchUserWithUID(uid: uid) { (user) in
self.fetchPostsWithUser(user: user)
}
}
fileprivate func fetchPostsWithUser(user: User) {
let ref = Database.database().reference().child("posts").child(user.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let dictionaries = snapshot.value as? [String: Any] else { return }
dictionaries.forEach({ (key, value) in
guard let dictionary = value as? [String: Any] else { return }
var post = Post(user: user, dictionary: dictionary)
post.id = key
self.posts.append(post)
})
}) { (err) in
print(err)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 300)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
cell.post = posts[indexPath.item]
cell.delegate = self
return cell
}
}
können Sie den Beitrag hinzufügen Array an jeden Beitrag, wenn es nicht im Preis enthalten. Sie können die ID des Posts innerhalb des Posts-Arrays überprüfen, um sie nicht zu duplizieren. – ridvankucuk
Bricht es noch, wenn Sie [reloadData()] (https://developer.apple.com/documentation/uikit/uicollectionview/1618078-reloaddata) in der Sammlungsansicht am Ende von 'handleRefresh()' aufrufen? – JeremyP
Wo ist removeAll Funktion Körper, Könnten Sie teilen? – dip