Reputation: 153
I am trying to make a query to find a user that matches a certain email. When I perform the query, I get all of the emails in the database. My final objective is to find this user with his or her email and then update the InMatch
and SearchingForUser
key values. This is my code:
func fetchUsersSearchingForMatch() {
ref = FIRDatabase.database().reference()
let user = AppUser()
ref.child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
user.userEmail = (snapshot.value as? NSDictionary)?["email"] as? String
user.searchingForMatch = ((snapshot.value as? NSDictionary)?["SearchingForMatch"] as? Bool)!
print(dictionary)
if user.searchingForMatch == true {
self.users.append(user)
self.foundUser = self.users[0]
print("this is your foundUser \(self.foundUser.userEmail)")
print("this number of users are searching for a match: \(self.users.count)")
}
}
self.foundUserSearchingForMatch()
})
}
func foundUserSearchingForMatch() {
if user.inMatch == false {
let query = self.ref.child("users").queryOrdered(byChild: "email").queryEqual(toValue: foundUser.userEmail)
print("this is the query \(query)")
}
}
I am not doing any updating until I know that I am selecting the right user. When I do, how do you update the values? Can I do it inside the query?
JSON TREE:
{"Firebase APP": {
"RANDOM USER ID": {
"USEREMAIL": "[email protected]",
"SearchingForMatch": "true",
"InMatch": "false" }
}
When the user finds a match, I want to change the InMatch
key to true
and the SearchingForMatch
to false
.
I don't want to update my email or the foundUser
email. I want to update the foundUser
, InMatch
, and SearchingForMatch
key values.
The way I get the email from the user looking for a match from FBDB is when I check if SearchingForMatch
equals true
in all of my users, if it does equal true
, I append it to an array and then get the first element's email (which is now the foundUser
). Using the foundUser
email, I want to update that users key values (InMatch
and SearchingForMatch
).
Upvotes: 1
Views: 1014
Reputation: 9945
Try This:-
FIRAuth.auth()?.currentUser?.updateEmail("new_Email", completion: { (Err) in
if Err == nil {
FIRDatabase.database().reference().child("Users").queryOrdered(byChild: "USEREMAIL").queryEqual(toValue: ).observeSingleEvent(of: .value, with: {(Snap) in
if let snapDict = Snap.value as? [String:AnyObject]{
for each in snapDict{
let key = each.key
let match = each.value["SearchingForMatch"] as! Bool
FIRDatabase.database().reference().child("Users/\(key)").updateChildValues(["SearchingForMatch" : "true"], withCompletionBlock: { (err, ref) in
if err == nil{
//Task acomplished....
}
})
}
}
})
}else{
//Couldn't update the auth value.
}
})
Upvotes: 1