I want to get a specific value from Firebase Database with child name "totalExercise" and print it using swift. Here is the current content of firebase database.
And here is my code in swift trying to get and print the totalExercise value
I want to get a specific value from Firebase Database with child name "totalExercise" and print it using swift. Here is the current content of firebase database.
And here is my code in swift trying to get and print the totalExercise value
This is a literal answer to your question but please read on
let loginRef = self.ref.child("fblogintest-35707").child("Optional(\"gl8...\")")
loginRef.observeSingleEvent(of: .value, with: { snapshot in
let val = snapshot.value as! [String: Any]
let te = val["totalExercise"]
print(te)
})
the gl8 is a long string which I didnt feel like retyping so just insert that from your question. This code will print '10'
That being said, there are a number of issues with the Firebase structure and the code in the question.
As you can see, whatever the code is that's creating the keys in your question is creating them as Optionals and that should not be done as the actual string of "Optional("gl8..") is being written to Firebase.. That's why you see the word Optional in your structure.
You should probably do some unwrapping/error checking on ref? and user? as well so if they are nil (they are optionals) the code doesn't crash.
If you haven't done so, read up on Optionals as they are super powerful but you got to wrap your brain around how to implement them effectively to protect your code.