Reputation: 33
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()
})
}
}
Upvotes: 1
Views: 134
Reputation: 299
override func viewDidLoad() {
super.viewDidLoad()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
self.refreshControl = refreshControl
requestData()
}
@objc func requestData() {
fetchNetworkPosts() //populates data
}
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
self.postArray.removeAll()
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.refresh()
})
}
}
inside fetchNetworkPosts()
method when you receive the new data
self.postArray.removeAll()
remove old data there and then assign new value at that time. then call refresh()
.
Hope this will work for you.
Upvotes: 1
Reputation: 5223
Call your refresh() after the completion of fetchNetworkPosts() like below. Check the code below.
@objc func requestData() {
fetchNetworkPosts() //populates data
}
func refresh() {
self.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
func fetchNetworkPosts() {
self.postArray.removeAll() // remove your array data here
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.refresh()
})
}
}
Upvotes: 1
Reputation: 71
maybe changing order will fix your problem.
override func viewDidLoad() {
super.viewDidLoad()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(requestData), for: .valueChanged)
self.refreshControl = refreshControl
requestData()
}
@objc
func requestData() {
fetchNetworkPosts() //populates data
refresh()
}
func refresh() {
self.postArray.removeAll()
self.refreshControl?.endRefreshing()
self.tableView.reloadData()
}
You are removing your data before fetch it. So, fetch data first then remove your tableView. You'll see your old data for brief time, not empty tableView.
Upvotes: 0