I've looked around and tried different queries but none seem to work.
I have a social networking iOS app. When I change a user’s profile image, I need all the previous posts made by this particular user to also update and show the new profile image. I have this current user's userId.
It seems the only way is to query all the posts made by this user and then loop over them and update the image url. But I’m not sure how to perform this query.
This is how the posts node of the database looks like.
posts
post1ID (e.g. -TaldfjWEFa92SASnKN)
name: ”Jack Johnson”
profileImageUrl: ”https://firebasestorage.googleapis.com/….”
userId: "UekUSYobEKZYO9WqXJQu2"
-asjfka1ADFAJ23klja=
name: "Karin Bold"
profileImageUrl: ”https://firebasestorage.googleapis.com/….”
userId: "YobEKZYIUxqXJQu2dl2AD"
And the relevant code:
if let imgData = UIImageJPEGRepresentation(img, 0.1) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
let imagesRef = DataService.instance.imagesStorageRef.child(imgUid)
imagesRef.putData(imgData, metadata: metadata, completion: { (metadata, error) in
if error != nil {
//show alert
} else {
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL {
changeRequest?.photoURL = URL(string: url)
changeRequest?.commitChanges() { (error) in
if error == nil {
//get all the posts created by this user id
//update profile image url in all of them
self.userImageUrlRef = DataHandler.instance.postsRef.child("userId").queryOrdered(byChild: AuthHandler.instance.currentUserId).ref //child(AuthHandler.instance.currentUserId).child("profileImgUrl")
self.userImageUrlRef.updateChildValues(["profileImageUrl" : url])
} else {
//show alert
}
}
}
}
})
}