Hello,
I'm working on this messaging app where users have the option of blocking other users. The problem I'm having is reloading the UITableView after a user has been blocked. The first block of code is to block the users:
MemberVC
func blockUserOption(forMember memberData: String, userHasBeenBlocked: Bool, completion: ((Error?) -> Void)?) {
BLOCKED_USERS_COLLECTION.document(currentUID!).getDocument { (snapshot, error) in
let data = [self.memberDocumentID: userHasBeenBlocked]
if snapshot?.exists == true {
BLOCKED_USERS_COLLECTION.document(self.currentUID!).updateData(data)
} else {
BLOCKED_USERS_COLLECTION.document(self.currentUID!).setData(data)
}}}
//Here is how I'm executing the code above:
let blockUser = UIAlertAction(title: "Block", style: .default) { (action) in
let alert = UIAlertController(title: "Block", message: "Are you sure you want to block" + " " + "\(self.memberName as Any)?", preferredStyle: .alert)
alert.view.tintColor = .brick
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction) in
self.blockUserOption(forMember: self.memberDocumentID, userHasBeenBlocked: true, completion: nil)
self.userDefaults.setValue("yes", forKey: RELOAD)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action: UIAlertAction!) in
}))
self.present(alert, animated: true, completion: nil)
}
Once the user has been blocked, the current view controller gets dismissed and the user goes back to the view controller where the table view needs to reload and remove the blocked user from the list.
MemberListVC
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
let reloadQuery = userDefaults.string(forKey: RELOAD)
if reloadQuery == "yes" {
self.match.removeAll()
let query = MATCH_INFO_COLLECTION.document(currentUID!)
.collection("info").order(by: TIMESTAMP, descending: false)
query.limit(to: 10).getDocuments { (snapshot, error) in
self.filterUsersThatWereBlocked { blockedUsers in
guard let last = snapshot?.documents.last else { return }
guard let snap = snapshot else { return }
snap.documents.forEach({ document in
let data = document.data()
let memberId = data[DOCUMENT_ID] as? String ?? ""
let memberAge = data[AGE] as? Int ?? 0
let memberName = data[FIRST_NAME] as? String ?? ""
let memberImageUrl = data[PROFILE_IMAGE_URL] as? String ?? ""
let memberCurrentCity = data[CURRENT_CITY] as? String ?? ""
let timestamp = data[TIMESTAMP] as? Double ?? 0.0
let matches = Match(memberId: memberId, memberAge: memberAge ,memberName: memberName, memberImageUrl: memberImageUrl, memberCurrentCity: memberCurrentCity, matchTimestamp: Date(timeIntervalSince1970: timestamp))
guard matches.memberId != Auth.auth().currentUser?.uid else { return }
guard blockedUsers[matches.memberId] == nil else { return }
self.match.append(matches)
self.matchedMessagesTV.reloadData()
})
self.lastDocument = last
}
}
userDefaults.setValue("no", forKey: RELOAD)
} else {
print("NOTHING TO DO HERE")
}
}
func filterUsersThatWereBlocked(completion: @escaping([String: Bool]) -> Void) {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
BLOCKED_USERS_COLLECTION.document(currentUid).getDocument { (snapshot, error) in
guard let data = snapshot?.data() as? [String: Bool] else {
completion([String: Bool]())
return
}
completion(data)
}}
Now, if I close and re-open the app, the users that were blocked, do not show up. I'm not sure if the issue is because I'm trying to reload the view inside "viewDidAppear", I tried with "viewWillAppear" and the outcome is the same. Any help is greatly appreciated. Thank you!
User is blocked in alert action
So that's very likely an async issue.
How do you transition to MemberListVC ? By dismissing ? Where is it done ?
What you should do is put this transition trigger in the handler of the Yes
button
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction) in
To be sure the transition is only triggered after user was blocked.