I'm working on a chat app and I configured a function to check if a user is online. I'm able to see if another user is active or not, but, the issue I'm having is that if I scroll down (I'm using a UITableView) other users show as active and they are not. I placed the code inside the UITableViewCell class. Any suggestions as to what could be the problem are greatly appreciated. Here is my code:
UITableViewCell
`func configureHomeFeedCell(member: Member) {
profileImage.loadImage(with: member.imageURL)
profileName.text = "\(member.name)" + ", " + "\(member.age)"
checkUserOnlineStatus(with: member.documentId) { _ in }
}
func checkUserOnlineStatus(with userId: String, completion: @escaping(Bool) -> Void) {
let query = USERS_COLLECTION.document(userId).collection(IS_ONLINE)
query.getDocuments { (snapshot, error) in
if let error = error {
print("ERROR..\(error.localizedDescription)")
} else {
snapshot?.documents.forEach({ diff in
let isOnline = diff.get(USER_IS_ONLINE) as? Bool
self.onlineViewStatus.backgroundColor = isOnline == true ? .green : .red
completion(isOnline!)
})}}
query.addSnapshotListener { (snapshot, error) in
snapshot?.documentChanges.forEach { diff in
let isOnline = diff.document.get(USER_IS_ONLINE) as? Bool
if (diff.type == .modified) {
self.onlineViewStatus.backgroundColor = isOnline == true ? .green : .red
completion(isOnline!)
}}}
}`