1

This is my first time ever using firebase, what I am trying to do is recall the 4 values stored in the database to individual strings heres the code and what my database looks like. I have enabled read and write in the database. Any help would be greatly appreciated.

I have gotten some code from here Type 'Any' has no subscript members (firebase)

and have been following a tutorial that stores data in a array https://youtu.be/XIQsQ2injLo

but so far I have been unsuccessful. Thanks

Firebase image

import UIKit
import Firebase
import FirebaseDatabase


class ViewController: UIViewController {

@IBOutlet var Silver: UILabel!
@IBOutlet var Aqua: UILabel!
@IBOutlet var Orange: UILabel!
@IBOutlet var Purple: UILabel!




@IBAction func loadData(_ sender: Any) {

    let databaseRef = FIRDatabase.database().reference()

    databaseRef.queryOrderedByKey().observe(.childChanged, with: {
        snapshot in


        let content2 = (snapshot.value as? NSDictionary)?["aqua"] as? String ?? ""
        let content3 = (snapshot.value as? NSDictionary)?["orange"] as? String ?? ""
        let content4 = (snapshot.value as? NSDictionary)?["purple"] as? String ?? ""
        let content = (snapshot.value as? NSDictionary)?["silver"] as? String ?? ""



        print(content2)
        print(content3)
        print(content4)
        print(content)

        self.Silver.text = content
        self.Aqua.text = content2
        self.Orange.text = content3
        self.Purple.text = content4
    })



}




override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let databaseRef = FIRDatabase.database().reference()

    databaseRef.queryOrderedByKey().observe(.childChanged, with: {
        snapshot in

        let content = (snapshot.value as? NSDictionary)?["silver"] as? String ?? ""
        let content2 = (snapshot.value as? NSDictionary)?["aqua"] as? String ?? ""
        let content3 = (snapshot.value as? NSDictionary)?["orange"] as? String ?? ""
        let content4 = (snapshot.value as? NSDictionary)?["purple"] as? String ?? ""


        self.Silver.text = content
        self.Aqua.text = content2
        self.Orange.text = content3
        self.Purple.text = content4
    })



}




}
Community
  • 1
  • 1
Yellow
  • 175
  • 1
  • 15

1 Answers1

1

You have used the .childChanged observer try below code

 ref.observeSingleEvent(of: .value, with: { (snapshot) in
  // Get user value
  let value = snapshot.value as? NSDictionary
  let username = value?["username"] as? String ?? ""


  // ...
  }) { (error) in
    print(error.localizedDescription)
}
harshal jadhav
  • 5,456
  • 2
  • 18
  • 26
  • Its now doing something, I think, it isn't pulling any values its just saying nothing – Yellow Apr 26 '17 at 09:41
  • I'm getting this error "2017-04-26 19:59:33.460986+1000 ScoreTally[10118:163608] [] nw_connection_get_connected_socket_block_invoke 9 Connection has no connected handler" – Yellow Apr 26 '17 at 10:00
  • the way you have data in your firebase is also not proper create one parent node inside that put all your values – harshal jadhav Apr 26 '17 at 10:13