So I have a tableView that has a header element and some cells in it. I am pulling the cells from the server and then reloading the tableview after I am done. Well that is the goal atleast right now I am reloading the tavleView after every single append to an array. I ultimately want to reload it only after all the data is pulled basically once.The function
self.fetchEventsFromServer()
handles the work of pulling the data. I read up on dispatchGroups and figured that would be the right way to go but I don't know how to go about doing it.
import UIKit
import Firebase
class FriendsEventsView: UITableViewController{
var cellID = "cellID"
var friends = [Friend]()
var followingUsers = [String]()
//label that will be displayed if there are no events
var currentUserName: String?
var currentUserPic: String?
var currentEventKey: String?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Friends Events"
view.backgroundColor = .white
// Auto resizing the height of the cell
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "close_black").withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(self.goBack))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
fetchEventsFromServer { (error) in
if error != nil {
print(error)
return
} else {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
@objc func goBack(){
dismiss(animated: true)
}
override func numberOfSections(in tableView: UITableView) -> Int {
print(friends.count)
return friends.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return friends[section].collapsed ? 0 : friends[section].items.count
return 1
}
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
cell.textLabel?.text = "Something to fill Section: \(indexPath.section) Row: \(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
print(section)
header.arrowLabel.text = ">"
header.setCollapsed(friends[section].collapsed!)
print(friends[section].collapsed!)
header.section = section
// header.delegate = self
header.friendDetails = friends[section]
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func fetchEventsFromServer(_ completion: @escaping (_ error: Error?) -> Void ){
//will grab the uid of the current user
guard let myUserId = Auth.auth().currentUser?.uid else {
return
}
let ref = Database.database().reference()
//checking database for users that the current user is following
ref.child("following").child(myUserId).observeSingleEvent(of: .value, with: { (followingSnapshot) in
//handling potentail nil or error cases
guard let following = followingSnapshot.children.allObjects as? [DataSnapshot]
else {return}
//validating if proper data was pulled
for followingId in following {
print(followingId.key)
ref.child("users").child(followingId.key).observeSingleEvent(of: .value, with: { (userInfoSnapShot) in
guard let followingUserInfo = userInfoSnapShot.children.allObjects as? [DataSnapshot] else {
return
}
//validating if proper data was pulled for each follower
for currentUserInfo in followingUserInfo {
if currentUserInfo.key == "username"{
self.currentUserName = currentUserInfo.value as! String
print(self.currentUserName)
var friend = Friend(friendName: self.currentUserName!, imageUrl: self.currentUserPic!)
self.friends.append(friend)
}
if currentUserInfo.key == "profilePic"{
self.currentUserPic = currentUserInfo.value as! String
print(self.currentUserPic)
}
}
}, withCancel: { (err) in
completion(err)
print("Couldn't grab info for the current list of users: \(err)")
})
completion(nil)
}
}) { (err) in
completion(err)
print("Couldn't grab people that you are currently following: \(err)")
}
completion(nil)
}
}
Any idea on how I would go about accomplishing this in swift it's really bugging me
Snapshot of following strucutre
"following" : {
"Iwr3EWqFBmS6kYRjuLW0Pw0CRJw2" : {
"CW1AIDxM43Ot3C1GtsyhQ0Zzwof2" : true
},
"OYWgNjYHEtX6EatkolPO5YXt6Rw2" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true,
"qYSDao0zhFbLrzd0IJBafi7qdis2" : true
},
"nRrGzLFt3TeN4OOrwTe0RjHQoF13" : {
"CW1AIDxM43Ot3C1GtsyhQ0Zzwof2" : true,
"XV62sIs7anaGaoo0Wr9kooC8FDP2" : true,
"r51UQXn4Q2WcPWIhXIG3dhZTHkX2" : true
},
"nbmheFEPmBerm5avZwnriGJkaK12" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true
},
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : {
"OYWgNjYHEtX6EatkolPO5YXt6Rw2" : true,
"qYSDao0zhFbLrzd0IJBafi7qdis2" : true
},
"qYSDao0zhFbLrzd0IJBafi7qdis2" : {
"nlSbmr1CXPbtuaUALNnftdHrbSt1" : true
},
"wdciX8B2LeUy2NyDwU5cjLog5xx2" : {
"72297UgQllfrEaAQnUCPKuQMv933" : true,
"nbmheFEPmBerm5avZwnriGJkaK12" : true
}
}