I'm displaying user profiles as search results in another view controller so that on selecting a cell the user gets followed. I load the results from updateSearchResults method and it loads well but when I select the row to follow that user sometimes it throws an error, Fatal error: Index out of range.
my code :
func updateSearchResults(for searchController: UISearchController) {
self.chooseResultsTableController.reloadData()
let ref = Database.database().reference()
let queryRef : DatabaseQuery
let lastUser = self.resultsUsers.last
guard searchController.searchBar.text != "" else {
return
}
queryRef = ref.child("users").queryOrdered(byChild: "full Name").queryStarting(atValue: searchController.searchBar.text).queryEnding(atValue: searchController.searchBar.text!+"\u{f8ff}").queryLimited(toLast: 20)
SVProgressHUD.show()
var tempUsers2 = [user]()
queryRef.observeSingleEvent(of: .value, with: { snapshot in
for snap in snapshot.children {
let userSnap = snap as! DataSnapshot
let userKey = userSnap.key
let userDict = userSnap.value as! [String:AnyObject]
let aUser = user()
aUser.fullName = userDict["full Name"] as? String
let joinedSince = userDict["dateJoined"] as! Double
aUser.ImagePath = userDict["urlToImage"] as? String
aUser.about = userDict["about"] as? String
if let FollowingCount = userDict["followers"] as? [String : String] {
aUser.followers = FollowingCount.values.count
}
aUser.userId = userKey
let sensibleDate = joinedSince / 1000
let date = Date(timeIntervalSince1970: sensibleDate)
var finalString = String()
let secondsAgo = Int(Date().timeIntervalSince(date))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
if secondsAgo < minute {
// finalString = "\(secondsAgo) Just now"
finalString = "Just now"
} else if secondsAgo < hour {
finalString = "\(secondsAgo / minute) minutes ago"
} else if secondsAgo < day {
finalString = "\(secondsAgo / hour) hours ago"
} else if secondsAgo < week {
finalString = "\(secondsAgo / day) days ago"
} else {
finalString = "\(secondsAgo / week) weeks ago"
}
aUser.joinedSince = finalString
aUser.untouchedDate = joinedSince
if userKey != lastUser?.userId {
tempUsers2.insert(aUser, at: 0)
print(tempUsers2)
// self.users.removeAll()
print("\(self.resultsUsers.count) number of users")
}
}
SVProgressHUD.dismiss()
self.resultsUsers = tempUsers2
self.chooseResultsTableController.reloadData()
})
}
Also, when I search with different keywords so that the table view data changes, the error seems to be guaranteed upon selection on the cell :(
the error code :
func checkFollowing(indexPath : IndexPath) {
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("users").child(uid!).child("following").queryOrderedByKey().observe(.value) { (snapshot) in
if let following = snapshot.value as? [String : AnyObject] {
for (_ , value) in following {
if value as? String == self.resultsUsers[indexPath.row].userId {
self.chooseResultsTableController.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
}
}
}