Simon Ho
Simon Ho

Reputation: 598

How to get a child value in firebase with swift 3.0

I want to display a string of "1.0" on an UITextField by reading the AppVersion Key from firebase My firebase JSON is simply the following

{
  "AppVersion" : "1.0"
}

My swift code (swift 3.0)

override func viewDidLoad() {
    super.viewDidLoad()

    ref = FIRDatabase.database().reference().child("AppVersion")
    AppVersionLabel.text = ref.key
}

The field display the word "AppVersion" instead of "1.0"

Upvotes: 1

Views: 2158

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598827

You need to attach an observer for the value.

ref.observe(FIRDataEventType.value, with: { (snapshot) in
  AppVersionLabel.text = snapshot.value as? String?
})

If you're only interested in the current version number (and not in receiving updates automatically), you can use observeSingleEvent with the same signature.

See https://firebase.google.com/docs/database/ios/read-and-write#listen_for_value_events

Upvotes: 1

Related Questions