Reputation: 3
I am new to the Swift and Firebase world. Recently I got stuck trying to sum specific field values in my Firebase database.
Here is a screen-grab of my db structure.
I would like to sum the "Number" values, but only if the
"Sender" = [email protected] (only the "[email protected]" sum of "Number" values)
I managed to sum all "number" values in the db, but filtering to a certain email values is too much of a challenge for me so far.
Could anyone help me out?
Upvotes: 0
Views: 60
Reputation: 471
you can do like this take local variable Num of integer type and execute following query of firebase. here yourref is ref to your Childs where all entry is placed.
yourref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let useremail = value?["Sender"] as? String ?? ""
if useremail == "[email protected]"{
//your local variable num
num = num + value?["Number"] as? Int ?? 0
}
// ...
}) { (error) in
print(error.localizedDescription)
}
Upvotes: 1