Leonif
Leonif

Reputation: 482

Can't get value of node in Firebase

My structure

I read a lot examples and most of them have old style (even they are written current year). Please help understand where my code is wrong? It is built but I can't get a value 123456.

import UIKit
import Firebase

class ViewController: UIViewController {
  @IBOutlet weak var val_txt: UITextField!
  let ref = FIRDatabase.database().reference()
  var barcode = 0  

  override func viewDidLoad() {
    super.viewDidLoad()
    FIRAuth.auth()?.signIn(withEmail: "*****@gmail.com", password: "*****", completion: {(user,error) in print("Авторизация Ок!!!")})

  }
  @IBAction func getData_btn(_ sender: Any) {
    ref.child("goods").child("1").observe(FIRDataEventType.value, with: {(snapshot) in
    let postDict = snapshot.value as? [String:AnyObject] ?? [:]
    print(postDict["barcode"] as? Int)
  })
  print(barcode)
}

I've change code in order to understand Does print execute and I found that it doesn't

print("Method started")
ref.child("goods").child("1").observe(FIRDataEventType.value, with:{(snapshot) in
    let postDict = snapshot.value as? [String:AnyObject] ?? [:]
    print("Method is executing")
  })
  print("Method completed")

And I get just two rows of print

"Method started"
"Method completed"

Upvotes: 1

Views: 531

Answers (3)

Parth Barot
Parth Barot

Reputation: 363

Use below code it will help you.

let ref = FIRDatabase.database().reference()
ref.child("goods").child("1").observe(DataEventType.value, with: {(snapshot) in
            if snapshot.childrenCount>0 {
                for transactions in snapshot.children.allObjects as! [DataSnapshot] {
                    var  obj = transactions.value as! [String:AnyObject]
                    print(obj["barcode"] as! Int)
                }
            }
})

Upvotes: 0

Hitesh Surani
Hitesh Surani

Reputation: 13577

In your code let ref = FIRDatabase.database().reference() do not Point to reference URL because during initialization your firebase not configure in your app delegate file (Not called FIRApp.configure()).

So put in func viewDidLoad() as follow:

import UIKit
import Firebase

class ViewController: UIViewController {
  @IBOutlet weak var val_txt: UITextField!
  let ref:FIRDatabaseReference!
  var barcode = 0  

  override func viewDidLoad() {
    super.viewDidLoad()
    ref:FIRDatabaseReference = FIRDatabase.database().reference()
    FIRAuth.auth()?.signIn(withEmail: "*****@gmail.com", password: "*****", completion: {(user,error) in print("Авторизация Ок!!!")})

  }

Upvotes: 1

Kulker22
Kulker22

Reputation: 396

If you want value only for "1":

var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.child("goods").child("1").observeSingleEvent(of: .value, with: { (snapshot) in
            let id = snapshot.childSnapshot(forPath: "barcode")
            print(id)
        })

but if you want all barcodes:

var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    ref.child("goods").observeSingleEvent(of: .value, with: { (snapshot) in
        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

            for snap in snapshots
            {
                let barcode = snap.childSnapshot(forPath: "barcode").value! as! String
                print(barcode)
            }
        }
    })

Upvotes: 2

Related Questions