I'm using a refreshcontrol, but while my code compiles, the current code has an issue where it removes all the data first and then pulls in the table data causing a brief moment where it isn't showing any data. Is there a quick fix for this? Should I use a completion handler for this by calling removeAll once the posts are loaded...
Edit: I've added the relevant portion of fetchNetworkPost below.
override func viewDidLoad() {
super.viewDidLoad()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
self.refreshControl = refreshControl
requestData()
}
@objc
func requestData() {
self.postArray.removeAll()
fetchNetworkPosts() //populates data
refresh()
}
func refresh() {
self.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
func fetchNetworkPosts() {
for personId in networkArray {
let currentUserRef =
Database.database().reference().child("posts").queryOrdered(byChild:
"userId").queryEqual(toValue: personId)
currentUserRef.observeSingleEvent(of: .value, with: {
(snapshot) in
for childSnapshot in snapshot.children {
let test = Post(snapshot: childSnapshot as! DataSnapshot)
self.postArray.append(test)
}
self.postArray.sort { $0.postDate > $1.postDate } //sort
self.tableView.reloadData()
})
}
}