I get the current friends of the users and save the users in an array that appears as a collection. The friends collection view is on my home controller, which means it always stays on the user stack .
My problem is that if the user adds or removes friends through the user profile page and then goes to the home view, I have to update the data and update the collection view in viewWillAppear. This makes the application expensive for the data, and I definitely think that it is not the most effective way.
I know that Firebase allows me to configure observers, but I'm not too sure about this without getting massive data.
This is how I currently get user friends
func fetchFriends() {
let userID = Auth.auth().currentUser?.uid
var tempFriend = [UserClass]()
self.usersArray.removeAll()
collectionView.reloadData()
let friendRef = self.databaseRef.child("users").child(userID!).child("Friends")
friendRef.queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
let friendID = "\(snapshot.value!)"
let usersRef = self.databaseRef.child("users")
usersRef.observeSingleEvent(of: .value, with: { (users) in
for user in users.children {
let friend = UserClass(snapshot: user as! DataSnapshot)
if friendID == friend.uid {
tempFav.append(friend)
}
}
self.usersArray = tempFav
self.collectionView.reloadData()
if self.usersArray.count == 0 {
self.noDataLabel.text = "You have no friends!"
self.noDataLabel.isHidden = false
} else {
self.noDataLabel.text = "Loading..."
self.noDataLabel.isHidden = true
}
})
})
}
This is triggered by the user. viewWillAppear
To make this question as clear as possible, what is the best way to update a user's friends without having to re-dial the entire list whenever a view appears.
source
share