所以我一直想要自己解决这个问题几个小时了。什么时候我拉动刷新它的崩溃说索引超出范围。有些人说它是因为在获取数据之前删除了数组数据而这一切只是让我更加困惑
这是我的代码
@objc fileprivate func fetchPostsWithUser() {
houses.removeAll()
myJobs.removeAll()
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
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(dictonary: dictionary)
post.id = key
Database.database().reference().child(key).observeSingleEvent(of: .value, with: { (snapshot) in
if post.pickedUserId == Auth.auth().currentUser?.uid {
self.myJobs.append(post)
} else if post.hasPicked == true && post.pickedUserId != Auth.auth().currentUser?.uid {
print("taken out")
} else {
self.houses.append(post)
}
self.houses.sort(by: { (p1, p2) -> Bool in
return p1.creationDate.compare(p2.creationDate) == .orderedDescending
})
self.tableView.reloadData()
}, withCancel: { (err) in
print("Failed to fetch like info for post:", err)
})
})
}) { (err) in
print("Failed to fetch posts:", err)
}
}
非常感谢您对此错误的解决方案,谢谢。